#include<cstdio>  
#include<iostream>  
#include<algorithm>  
#include<cstdlib>  
#include<cstring>
#include<string>
#include<climits>
#include<vector>
#include<cmath>
#include<map>
#define LL long long
#define pii pair<int,int>
#define mp make_pair
 
using namespace std;
 
inline char nc(){
  static char buf[100000],*p1=buf,*p2=buf;
  if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; }
  return *p1++;
}
 
inline void read(int &x){
  char c=nc();int b=1;
  for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
  for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}
 
inline void read(LL &x){
  char c=nc();LL b=1;
  for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
  for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}
 
inline int read(char *s)
{
	char c=nc();int len=1;
	for(;!(c>='a' && c<='z');c=nc()) if (c==EOF) return 0;
	for(;(c>='a' && c<='z');s[len++]=c,c=nc());
	s[len++]='\0';
	return len;
}
 
inline void read(char &x){
  for (x=nc();!(x>='A' && x<='Z');x=nc());
}
 
int wt,ss[19];
inline void print(int x){
	if (x<0) x=-x,putchar('-'); 
	if (!x) putchar(48); else {
	for (wt=0;x;ss[++wt]=x%10,x/=10);
	for (;wt;putchar(ss[wt]+48),wt--);}
}
inline void print(LL x){
	if (x<0) x=-x,putchar('-');
	if (!x) putchar(48); else {for (wt=0;x;ss[++wt]=x%10,x/=10);for (;wt;putchar(ss[wt]+48),wt--);}
}

int n,m,f[100010];
LL A,B,C,S;
struct data
{
	int x,y;
}a[100010];
vector<int> b[100010],c[100010];
bool v[100010];

bool check(int x)
{
	v[x]=1;bool flag=1;S+=b[x].size()+c[x].size();
	if (f[x]==1)
	{
		for (int i=0;i<b[x].size();i++)
			if (f[b[x][i]]==0) f[b[x][i]]=2,B++;
			else if (f[b[x][i]]!=2) flag&=0;
		for (int i=0;i<c[x].size();i++)
			if (f[c[x][i]]==0) f[c[x][i]]=3,C++;
			else if (f[c[x][i]]!=3) flag&=0;
		for (int i=0;i<b[x].size();i++)
			if (!v[b[x][i]]) flag&=check(b[x][i]);
		for (int i=0;i<c[x].size();i++)
			if (!v[c[x][i]]) flag&=check(c[x][i]);
	}
	else if (f[x]==2)
	{
		for (int i=0;i<b[x].size();i++)
			if (f[b[x][i]]==0) f[b[x][i]]=3,C++;
			else if (f[b[x][i]]!=3) flag&=0;
		for (int i=0;i<c[x].size();i++)
			if (f[c[x][i]]==0) f[c[x][i]]=1,A++;
			else if (f[c[x][i]]!=1) flag&=0;
		for (int i=0;i<b[x].size();i++)
			if (!v[b[x][i]]) flag&=check(b[x][i]);
		for (int i=0;i<c[x].size();i++)
			if (!v[c[x][i]]) flag&=check(c[x][i]);
	}
	else
	{
		for (int i=0;i<b[x].size();i++)
			if (f[b[x][i]]==0) f[b[x][i]]=1,A++;
			else if (f[b[x][i]]!=1) flag&=0;
		for (int i=0;i<c[x].size();i++)
			if (f[c[x][i]]==0) f[c[x][i]]=2,B++;
			else if (f[c[x][i]]!=2) flag&=0;
		for (int i=0;i<b[x].size();i++)
			if (!v[b[x][i]]) flag&=check(b[x][i]);
		for (int i=0;i<c[x].size();i++)
			if (!v[c[x][i]]) flag&=check(c[x][i]);
	}
	return flag; 
}

LL calc()
{
	memset(f,0,sizeof(f));
	for (int i=1;i<=m;i++)
		b[a[i].x].push_back(a[i].y),
		c[a[i].y].push_back(a[i].x);
	LL ans=0;
	for (int i=1;i<=n;i++)
		if (b[i].size()>0 && f[i]==0)
		{
			f[i]=1;A=1,B=0,C=0;S=0;
			memset(v,0,sizeof(v));
			if (!check(i)) 
				ans+=(A+B+C)*(A+B+C);
			else if (C==0) ans+=S/2;
			else ans+=(A*B)+(B*C)+(C*A);
		}
	return ans;
}

int main()
{
	read(n);read(m);
	for (int i=1;i<=m;i++)
		read(a[i].x),read(a[i].y);
	print(calc());puts("");
	return 0;
}

Measured memory_bytes: 18816