Problem: We have a grid with N rows and N columns. The cell at the i-th row and j-th column is denoted (i, j).

Initially, M of the cells are painted black, and all other cells are white. Specifically, the cells (a_1, b_1), (a_2, b_2), ..., (a_M, b_M) are painted black.

Snuke will try to paint as many white cells black as possible, according to the following rule:

* If two cells (x, y) and (y, z) are both black and a cell (z, x) is white for integers 1≤x,y,z≤N, paint the cell (z, x) black.



Find the number of black cells when no more white cells can be painted black.

Constraints

* 1≤N≤10^5
* 1≤M≤10^5
* 1≤a_i,b_i≤N
* All pairs (a_i, b_i) are distinct.

Input

The input is given from Standard Input in the following format:


N M
a_1 b_1
a_2 b_2
:
a_M b_M


Output

Print the number of black cells when no more white cells can be painted black.

Examples

Input

3 2
1 2
2 3


Output

3


Input

2 2
1 1
1 2


Output

4


Input

4 3
1 2
1 3
4 4


Output

3

Solution (in CPP):

Solution:
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10;
typedef long long ll;
int gi() {
    int x=0,o=1;char ch=getchar();
    while((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
    if(ch=='-') o=-1,ch=getchar();
    while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x*o;
}
struct edge { int to,w,nxt; } e[N<<1];
int tot=1,h[N];
void adde(int u,int v) {
    e[++tot]=(edge){v,1,h[u]};h[u]=tot;
    e[++tot]=(edge){u,-1,h[v]};h[v]=tot;
}
int n,m,V,E,col[N],cnt[3];
bool vis[N],vise[N];
ll ans=0;
bool flag=0;
void dfs(int u) {
    V++;vis[u]=1;
    cnt[col[u]]++;
    for(int i=h[u];i;i=e[i].nxt) {
        if(vise[i>>1]) continue; vise[i>>1]=1,E++;
        int v=e[i].to;
        if(!vis[v]) col[v]=(col[u]+e[i].w+3)%3,dfs(v);
        else if(col[v]!=(col[u]+e[i].w+3)%3) flag=1;
    }
}
int main() {
	//freopen("flower.in","r",stdin);
	//freopen("flower.out","w",stdout);
    cin>>n>>m;
    for(int i=1,u,v;i<=m;i++) u=gi(),v=gi(),adde(u,v);
    for(int i=1;i<=n;i++)
        if(!vis[i]) {
            V=E=cnt[0]=cnt[1]=cnt[2]=0;flag=0;dfs(i);
            if(flag) ans+=1ll*V*V;
            else if(!cnt[0]||!cnt[1]||!cnt[2]) ans+=E;
            else ans+=1ll*cnt[0]*cnt[1]+1ll*cnt[1]*cnt[2]+1ll*cnt[2]*cnt[0];
        }
    printf("%lld\n",ans);
    return 0;
}