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

// BFS
int main(void){
    int n, m;
    cin >> n >> m;
    vector<vector<int>> to(n);
    rep(i,m) {
        int a, b;
        cin >> a >> b;
        --a; --b;
        to[a].push_back(b);
    }
    
    int ans = 0;
    rep(sv,n) {
        queue<int> q;
        vector<int> used(n);
        used[sv] = 1; //既に訪れた点は計算しない
        ++ans;
        q.push(sv);
        while (q.size()) {
            int v = q.front(); q.pop();
            //cout << "----" << endl;
            for (int u : to[v]) { //範囲列挙 to[0]なら1で行先
                //cout << u << endl;
                if (used[u]) continue;
                ++ans;
                used[u] = 1;
                q.push(u);
            }
        }
    }
    cout << ans << endl;
}

#include<bits/stdc++.h>

using namespace std;

vector<int>graph[3000];
bool visited[3000];

long long int dfs(int index){
    visited[index] = true;
    int cnt = 1;
    for (auto i:graph[index])
        if (!visited[i])
            cnt+=dfs(i);
    return cnt;
}

int main(){
    int n,m;
    cin>>n>>m;

    for (int i=0;i<m;i++){
        int a,b; cin>>a>>b;
        graph[a-1].push_back(b-1);
    }

    long long int ans = 0;
    for (int i=0;i<n;i++){
        for (int j=0;j<n;j++) visited[j] = false;
        ans+=dfs(i);
    }

    cout<<ans<<endl;
	return 0;
}