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

#define sz(a) int((a).size())
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(), (c).end()
#define tr(c, i) for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)

#define uns(v) sort((v).begin(), (v).end()), v.erase(unique(v.begin(), v.end()), v.end())

#define inf 1000000000
#define MAXN 2007
#define MOD 1000000007
typedef long long ll;
bool vis[MAXN];
vector<int> adj[MAXN];
int cnt;
void dfs(int u) {
    if (!vis[u]) {
        vis[u] = true;
        cnt++;
        for (int i = 0; i < sz(adj[u]); i++) {
            int v = adj[u][i];
            if (vis[v]) continue;
            dfs(v);
        }
    }
}
void _run_test() {
    int n, m, a, b;
    cin >> n >> m;
    for (int i = 0; i < m; i++) {
        cin >> a >> b;
        a--, b--;
        adj[a].pb(b);
    }
    ll res = 0;
    for (int i = 0; i < n; i++) {
        memset(vis, false, sizeof(vis));
        cnt = 0;
        dfs(i);
        res += cnt;
    }
    cout << res << "\n";
}
int main() {
    std::ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t = 1;
    // cin >> t;
    while (t--) {
        _run_test();
    }

    return 0;
}

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define irep(i, n) for (int i = (n-1); i >= 0; i--)
using namespace std;
using ll = long long;
using P = pair<int,int>;

constexpr int N = 2000;
vector<vector<int>> g(N);

int bfs(int start, int n) {
	vector<int> dist(N, -1);
	queue<int> q;

	dist[start] = 0;
	q.push(start);

	while (!q.empty())
	{
		int v = q.front();
		q.pop();

		for (int nv : g[v]) {
			if (dist[nv] != -1) continue;

			dist[nv] = dist[v] + 1;
			q.push(nv);
		}
	}
	
	int r = 0;
	rep(i,n) {
		if (dist[i] >= 0) r++;
	}
	return r;
}


int main() {
	int n, m;
	cin >> n >> m;
	rep(i,m) {
		int a, b;
		cin >> a >> b;
		a--; b--;
		g[a].push_back(b);
	}

	int ans = 0;
	rep(i,n) {
		ans += bfs(i, n);
	}
	cout << ans << endl;
}