#include<bits/stdc++.h>
#define mod 1000000007
#define pi pair<int, int>
#define ll long long int
#define pll pair<ll, ll>
#define ppll pair<ll, pll>
#define vi vector<int>
#define vll vector<ll>
#define vpi vector<pi>
#define vpll vector<pll>
#define vppll vector<pair<ll, pll>>
#define vvll vector<vector<ll>>
#define vvpll vector<vector<pll>>
#define repi(i,a,n) for(ll i=a;i<n;i++)
#define repr(i,a,b) for(ll i=a;i>=b;i--)
#define pb push_back
#define ff first
#define ss second
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define pr(i,v) for(auto i:v)
static int row[4] = {-1, 0, 0, 1};
static int col[4] = {0, -1, 1, 0};
static int dagb[4] = {1, -1, 1, -1};
static int daga[4] = {1, -1, -1, 1};

using namespace std;
vll a[2001];
vll vis(2001,0);
vll d(2001,0);
ll dfs(ll u){
     vis[u]=1;
     int curr=0;
     for(auto i: a[u]){
        if(vis[i]==0){
            curr+=dfs(i);

        }
     }
     return curr+1;
}


int main( ) 
{

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
     #ifndef ONLINE_JUDGE
        freopen("input.txt" , "r"  ,stdin);
        freopen("ott.txt" , "w" ,stdout);
        freopen("error.txt" , "w" ,stderr);
     #endif 

     
      ll t=1;
     //cin>>t;
   while(t--){ 
    string s;
    ll l=1,n,m,w,flag=0,y=0,z=0,x=0,k, mn=1e9+7,d,e=0,f=INT_MAX,sum=0,ans=0,de=1,mx=0,g;
      cin>>n>>m;
      repi(i,0,m){
         cin>>x>>y;
         a[x].pb(y);
      }
      repi(i,1,n+1){
         vis.assign(n+1, 0);
         sum+=dfs(i);
      }
       cout<<sum<<endl;


       }
   return 0; 
}  
/* 	author : sgupta_2001 	*/

#include<bits/stdc++.h>

using namespace std;

using db = double;
using ll = long long;
using ld = long double;
using ull = unsigned long long;

//containers
#define sz(x) int((x).size())
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define rall(x) x.rbegin(), x.rend() 
#define sor(x) sort(all(x)) 
#define rsz resize
#define ins insert 
#define ft front()
#define bk back()
#define pb push_back
#define eb emplace_back 
#define pf push_front

//pairs
#define mp make_pair
#define ff first
#define ss second

//loops
#define rep(i, a, b) for(int i = a; i < b; ++i)
#define per(i, a, b) for(int i = b - 1; i >= a; --i)
#define each(x, a) for(auto &x : a)

//popular constants
const int mod = 1e9 + 7; //998244353;
const ll inf = 1e18;
const ld pie = acos((ld) - 1);

//grid problems
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

//modulo operations
ll mod_add(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a + b) % m) + m) % m;}
ll mod_mul(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a * b) % m) + m) % m;}
ll mod_sub(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a - b) % m) + m) % m;}

#ifdef SGUPTA_2001
#define deb(x) cerr<< #x <<" "; __print(x); cerr<<'\n';
#else
#define deb(x);
#endif

//debug containers
template<class T> void __print(T x) {cerr<<x;}
template<class T, class U> void __print(pair<T,U> p) {cerr<<"{"<<p.ff<<", "<<p.ss<<"}";}
template<class T> void __print(vector<T> arr) { cerr<<"[ "; for(auto x:arr) { __print(x);cerr<<" "; } cerr<<"]"; }
template<class T> void __print(set<T> st) { cerr<<"{ "; for(auto x:st) { __print(x); cerr<<" "; } cerr<<"}";}
template<class T, class U> void __print(vector<pair<T,U>> arr) { cerr<<"[ "; for(auto x:arr) {__print(x);cerr<<", ";}cerr<<"]";}
template<class T, class U> void __print(unordered_map<T,U> arr) { cerr<<"{ "; for(auto x:arr) {__print(x);cerr<<", ";}cerr<<"}";}
template<class T, class U> void __print(map<T,U> arr) { cerr<<"{ "; for(auto x:arr) {__print(x);cerr<<", ";}cerr<<"}";}
template<typename... T> void read(T&... args) { ((cin>>args),...);}
template<typename... T> void write(T&&... args){((cout<<args<<" "),...);cout<<'\n';}

const int MAXN = 2002;

vector<int> gr[MAXN];
int t_in[MAXN], t_out[MAXN];
int vis[MAXN];
int timer;

void dfs(int u, int par) {

	t_in[u] = ++timer;

	vis[u] = 1;

	for(auto x : gr[u]) {
		if(x != par && !vis[x]) {
			dfs(x, u);
		}
	}

	t_out[u] = timer;
	return;
}

void solve() {

	int N, M;
	scanf("%d%d", &N, &M);

	rep(i, 0, M) {
		int X, Y;
		scanf("%d%d", &X, &Y);

		X--;Y--;

		gr[X].pb(Y);
	}

	ll ans = 0;

	rep(i, 0, N) {
		dfs(i, -1);
		fill(vis, vis + N + 1, 0);
		
		int p = t_in[i], q = t_out[i];
		deb(mp(p, q));
		int cnt = 0;
		rep(j, 0, N) {
			// printf("%d %d %d\n", j + 1, t_in[j], t_out[j]);
			if(p <= t_in[j] && q >= t_out[j]) {
				ans++;
			}
		}	

		fill(t_in, t_in + N + 1, 0);
		fill(t_out, t_out + N + 1, 0);
		timer = 0;
		// ans += cnt*(cnt + 1) >> 1;
	}
	
	printf("%lld\n", ans);

	return;
}


int main() {

	#ifdef SGUPTA_2001
		freopen("input.txt", "r", stdin);
		freopen("error.txt", "w", stderr);
	#endif

	auto start = std::chrono::high_resolution_clock::now();

	int t;
	t=1;
	while(t--) solve();

  auto stop = std::chrono::high_resolution_clock::now(); 
  auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(stop - start);
  // cerr << "Time taken : " << ((long double)duration.count())/((long double) 1e9) <<"s "<< '\n';

	 /* 
	 		STUFF TO LOOK:	
			1. Check the constraints
			2. Corner cases(n == 0 || n == 1)
			3. Variables in loops
			4. Make sure two ints aren’t multiplied to get a long long 
	*/  

	return 0;
}
