#include <bits/stdc++.h> 
using namespace std; 
vector<int> ar[2001];
set<pair<int,int>> s;
int visited[2001];
void dfs(int x,int source)
{
	visited[x]=1;
	for(auto it:ar[x])
	{
		if(!visited[it])
		{
			s.insert({source,it});
			dfs(it,source);

		}
	}
}
void solve()
{
int t=1;
//cin>>t;
while(t--)
{
int n,m,i;
cin>>n>>m;
int x,y;
for(i=1;i<=n;i++)
{
	s.insert({i,i});	
}
for(i=0;i<m;i++)
{
	cin>>x>>y;
	ar[x].push_back(y);


}

for(i=1;i<=n;i++)
{
	memset(visited,0,sizeof(visited));
	dfs(i,i);
}
cout<<s.size()<<endl;


}
 
}
int main()
{
  ios::sync_with_stdio(0);
cin.tie(0); 
solve();
cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" secs"<<endl; 
return 0;
} 
#include "bits/stdc++.h"
using namespace std;
using ll=long long;
using vi=vector<int>;
using vvi=vector<vi>;
using vll=vector<ll>;
using vvll=vector<vll>;
using pii=pair<int,int>;
#define rep(i,n) for(int i=0;i<(n);i++)
#define sor(v) sort(v.begin(),v.end())
#define rev(v) reverse(v.begin(),v.end())
#define pb push_back
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
//const int INF = 1123456789;
//const int mod = 1e9+7;
int n,m;
vvi g;
int ans=0;
vector<bool> checked;
void f(void){
    checked.clear();
    checked.resize(n);
}
void dfs(int now){
    checked[now]=true;
    for(auto a:g[now]) if(!checked[a]) dfs(a);
}
int main(){
    cin>>n>>m;
    g.resize(n);
    rep(i,m){
        int a,b; cin>>a>>b;
        a--; b--;
        g[a].pb(b);
    }
    rep(i,n){
        f();
        dfs(i);
        rep(j,n) if(checked[j]) ans++;
        
    }
    
    cout<<ans<<endl;
}
