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
#include<bits/stdc++.h>
#define fo(i,l,r) for(ll i=l;i<=r;i++)
#define fe(i,u) for(ll i=head[u];i;i=e[i].next)
using namespace std;
typedef long long ll;
const ll N=100010;
struct edge{ ll v,ty,next; edge(ll v=0,ll ty=0,ll next=0):v(v),ty(ty),next(next){} }e[N<<1];
ll n,m,col[N],cnt[3],siz; ll tot=0,head[N];
bool flag;
inline void add(ll u,ll v,ll ty){e[++tot]=edge(v,ty,head[u]);head[u]=tot;}
void dfs(ll u) { cnt[col[u]]++; fe(i,u){ ll v=e[i].v,ty=e[i].ty; siz+=(ty==1); if(~col[v])flag|=(col[v]!=(col[u]+ty)%3); else col[v]=(col[u]+ty)%3,dfs(v); } }
int main() {
	scanf("%lld%lld",&n,&m); fo(i,1,m){ ll x,y; scanf("%lld%lld",&x,&y); add(x,y,1);add(y,x,2); }
    memset(col,-1,sizeof col); ll ans=0;
    fo(i,1,n){
        if(~col[i])continue; cnt[col[i]=0]=cnt[1]=cnt[2]=siz=flag=0; dfs(i);
        if(flag)ans+=(cnt[0]+cnt[1]+cnt[2])*(cnt[0]+cnt[1]+cnt[2]); else if(cnt[0]&&cnt[1]&&cnt[2])ans+=cnt[0]*cnt[1]+cnt[1]*cnt[2]+cnt[2]*cnt[0]; else ans+=siz;
    }
    printf("%lld\n",ans);
    return 0;
}