#include<bits/stdc++.h>
using namespace std;
 
//#Rohitpratap311
//#Keep Calm And say Happy

using ll = long long;
using ld = long double;

vector<ll> adj[2001];

void dfs(ll v,ll &x,vector<bool> &vis)
{
    x++;
    vis[v]=1;
    for(auto u:adj[v])
    {
        if(!vis[u]) dfs(u,x,vis);
    }
}

void solveCP311()
{
    ll n;
    cin>>n;
    ll m;
    cin>>m;
    while(m--)
    {
        ll u,v;
        cin>>u>>v;
        adj[u].push_back(v);
    }
    ll ans=0;
    for(ll i=1;i<=n;i++)
    {
        vector<bool> vis(n+1,0);
        ll x=0;
        dfs(i,x,vis);
        ans+=x;
    }
    cout<<ans;
}
 
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    int t=1;
   // cin>>t;
    while(t--)
    {
        solveCP311();
    }
    return 0;
}

#include "bits/stdc++.h"
using namespace std;

#define int long long
#define pb push_back
#define f(i,a,n) for(int i=a ; i<n ; i++)
#define rf(i,n,a) for(int i=n ; i>=a ; i--)
#define F first
#define S second
#define all(c) (c).begin(),(c).end()
#define sz(v) (int)(v).size()
#define fast ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;

const int inf = 1e9;
const int inf64 = 1e18;
const int MOD = inf + 7;
const int N = 2005;
vi g[N];
vi vis(N, 0);
int ans = 0;
void dfs(int node){
    ans++;
    vis[node] = 1;
    for(int u : g[node]){
        if(!vis[u]){
            dfs(u);
        }
    }
}
void solve() {
    int n,m;
    cin >> n >> m;

    f(i,0,m){
        int u,v;
        cin >> u >> v;
        if(u != v){
            g[u].pb(v);
        }
    }

    f(i,1,n+1){
        f(j,1,n+1) vis[j] = 0;
        dfs(i);
    }
    cout << ans;
}

int32_t main() {
    fast;
    int t = 1;
    // cin >> t;
    while (t--)  solve();
}