#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;
}
#include <bits/stdc++.h>

using namespace std;
const int N = int(2e5) + 2;
int cnt;
bool cycle, vis[2*N];
vector<int> G[2*N];

void DFS(int u, int par)
{
    vis[u] = 1;
    ++cnt;
    int chk = 1;
    for(const int &v : G[u]) {
        if(v == par && chk) {
            --chk;
            continue;
        }
        if(!vis[v]) DFS(v, u);
        else cycle = true;
    }
}

int main()
{
    //freopen("input", "r", stdin);
    ios::sync_with_stdio(false); cin.tie(0);
    int n;
    cin >> n;
    for(int i = 1; i <= n; ++i) {
        int u, v;
        cin >> u >> v;
        G[u].push_back(v);
        G[v].push_back(u);
    }
    int res = 0;
    for(int i = 1; i < 2 * N; ++i) {
        if(!vis[i]) {
            cnt = 0;
            cycle = false;
            DFS(i, i);
            if(cycle) res += cnt;
            else res += cnt - 1;
        }
    }
    cout << res;
    return 0;
}
