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

typedef long long ll;
typedef long double ld;
const ll mod = 1e9 + 7;
ll ans = 0;

void dfs(ll p, vector<vector<ll>> &v, vector<bool> &visited)
{
    for (ll i : v[p])
    {
        if (!visited[i])
        {
            // cout << "( " << p << " , " << i << " ) ";
            ans++;
            visited[i] = 1;
            dfs(i, v, visited);
        }
    }
}

void solve()
{
    ll n, m;
    cin >> n >> m;
    vector<vector<ll>> v(n + 1);
    for (ll i = 0; i < m; ++i)
    {
        ll a, b;
        cin >> a >> b;
        v[a].push_back(b);
    }

    for (ll i = 1; i <= n; ++i)
    {
        ans++;
        if (v[i].size() == 0)
            continue;
        vector<bool> visited(n, 0);
        visited[i] = 1;
        dfs(i, v, visited);
        cout << endl;
    }
    cout << ans << endl;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    //   ll t;
    //   cin >> t;
    //   while (t--)
    //   {
    cout << setprecision(15) << fixed;
    solve();
    //   }
    return 0;
}
#include <bits/stdc++.h>
#define rep(i,l,r) for(int i = (l); i <= (r); ++i)
#define per(i,r,l) for(int i = (r); i >= (l); --i)
using namespace std;
typedef long long ll;
inline int gi() {
	int f = 1, x = 0; char ch = getchar();
	while (ch < '0' || ch > '9') {if (ch == '-') f = -f;ch = getchar();}
	while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
	return f * x;
}
const int N = 2010;
int n, m;
vector<int> g[N];
int vis[N];
void dfs(int u) {
	vis[u] = 1;
	for(auto& v: g[u]) if (!vis[v]) dfs (v);
}
int main(){
	n = gi(), m = gi();
	rep (i, 1, m) {
		int u = gi(), v = gi();
		g[u].push_back(v);
	}
	int ans = 0;
	rep (i, 1, n) {
		memset(vis, 0, sizeof (vis));
		dfs (i);
		ans += count(vis + 1, vis + 1 + n, 1);
	}
	
	cout << ans;
	return 0;
}




