#include <bits/stdc++.h>
#include <variant>

#define rep2(i,k,n) for(i64 i=(i64)(k);i<(i64)(n);i++)
#define rep(i,n) rep2(i,0,n)
#define all(x) begin(x),end(x)
#ifdef ENV_LOCAL
#define dump if (1) cerr
#else
#define dump if (0) cerr
#endif

using namespace std;
using namespace std::string_literals;

using i32 = int32_t;
using i64 = int64_t;
using f64 = double;
using f80 = long double;
using vi32 = vector<i32>;
using vi64 = vector<i64>;

//using namespace harudake;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  i32 n,m;
  cin>>n>>m;
  vector<vi32> g(n);
  rep(i,m) {
    i32 a,b;
    cin>>a>>b;
    --a;--b;
    g[a].push_back(b);
  }
  i32 ans = 0;
  rep(i,n) {
    vector<bool> vis(n, false);
    queue<i32> q;
    q.push(i);
    vis[i] = true;
    while (!q.empty()) {
      i32 j = q.front();
      q.pop();
      for (auto&& to : g[j]) {
        if (!vis[to]) {
          q.push(to);
          vis[to] = true;
        }
      }
    }
    ans += count(all(vis), true);
  }
  cout<<ans<<endl;
  return 0;
}

#include <bits/stdc++.h>
#define rep(i,a,b) for(int i = (a); i <= (b); i++)
#define rng(a) a.begin(), a.end()
#define ina(n,a) cin >> n; for(int i = 1; i <= n; i++) cin >> a[i]
#define sz(x) (int)(x).size()
#define se second
#define fi first
#define prev            coyhhhhhhyoc    
#define next          sdNNNmNNNNNNNmds  
#define y0           hNNNNy_yNNNNNN_sNh
#define y1          mNNNNNdtdNNNNNNtsNNm
#define yn          mNNNNNNNNy___smNNNms
#define tm            oooooosyysooooot 
#define read               tyhyt        
#define rank              ytmNmo         
#define index              yyy
#define pb push_back
#define pcnt __builtin_popcountll
#define rrep(i,a,b) for(int i = (b); i >= (a); i--)
#define rall(x,a) for(auto x : a)
#define MOD 1000000007
#define endl "\n"
typedef long long ll;
using namespace std;

const int N = 2222;
vector<int> graph[N];
bool vis[N];
int n, m, ans;

void dfs(int u) {
    if(vis[u]) return;
    vis[u] = true;
    ans++;
    rall(v, graph[u]) {
        dfs(v);
    }
}

int solve() {
    cin >> n >> m;
    rep(i, 1, m) {
        int u, v;
        cin >> u >> v;
        graph[u].pb(v);
    }
    rep(u, 1, n) {
        fill(vis + 1, vis + n + 1, 0);
        dfs(u);
    }
    cout << ans << endl;
    return 0;
}
 
signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int t = 1;
    while(t--) {
        solve();
    }
    return 0;
}
