// Author: wlzhouzhuan
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define pb push_back
#define fir first
#define sec second
#define rep(i, l, r) for (int i = l; i <= r; i++)
#define per(i, l, r) for (int i = l; i >= r; i--)
#define mset(s, t) memset(s, t, sizeof(s))
#define mcpy(s, t) memcpy(s, t, sizeof(t))
#define poly vector<int>
#define SZ(x) (int(x.size()))
template<typename T1, typename T2> void ckmin(T1 &a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> void ckmax(T1 &a, T2 b) { if (a < b) a = b; }
int read() {
  int x = 0, f = 0; char ch = getchar();
  while (!isdigit(ch)) f |= ch == '-', ch = getchar();
  while (isdigit(ch)) x = 10 * x + ch - '0', ch = getchar();
  return f ? -x : x;
}
template<typename T> void print(T x) {
  if (x < 0) putchar('-'), x = -x;
  if (x >= 10) print(x / 10);
  putchar(x % 10 + '0');
}
template<typename T> void print(T x, char let) {
  print(x), putchar(let);
}

const int N = 5005;
vector<int> adj[N];
bool vis[N];
int n, m;

int main() {
  n = read(), m = read();
  rep(i, 1, m) {
    int u = read(), v = read();
    adj[u].pb(v);
  }
  int num = 0;
  rep(i, 1, n) {
    rep(j, 1, n) vis[j] = 0;
    queue<int> q;
    q.push(i), vis[i] = 1;
    while (!q.empty()) {
      ++num;
      int u = q.front(); q.pop();
      for (auto v: adj[u]) {
        if (!vis[v]) {
          vis[v] = 1, q.push(v);
        }
      }
    }
  }
  print(num, '\n');
  return 0;
}
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
using Graph = vector<vector<ll>>;

vector<bool> seen;
ll dfs(const Graph &G, ll v, ll cnt) {
    seen[v] = true;
    ll res = cnt;

    for (auto next_v : G[v]) {
        if (seen[next_v]) continue;
        res = dfs(G, next_v, res)+1;
    }
    return res;
}


int main()
{
    ll N,M;
    cin >> N >> M;

    Graph G(N);
    for (ll i = 0; i < M; i++)
    {
        ll a, b;
        cin >> a >> b;
        a--;b--;
        G[a].push_back(b);
    }

    ll ans = 0;
    for(ll i = 0; i < N; i++){
        seen.assign(2010, false);
        ans += dfs(G,i,0);
    }

    cout << ans + N << endl;

    return 0;
}