#include <bits/stdc++.h>

using namespace std;

const int N = 2e3 + 5;

int n, m, a, b, ans;

bool vis[N];

vector<int> graph[N];

void dfs(int v){
    ans++;
    vis[v] = true;

    for(auto i : graph[v]){
        if(!vis[i]) dfs(i);
    }
}

int main(){
    ios_base::sync_with_stdio(false);cin.tie(NULL);
    
    cin >> n >> m;

    for(int i = 0; i < m; i++){
        cin >> a >> b;

        graph[a].push_back(b);
    }

    for(int i = 1; i <= n; i++){
        memset(vis, 0, sizeof(vis));

        dfs(i);
    }

    cout << ans;
}
#include <stdio.h>
#include <string>
#include <algorithm>
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n); i++)
using ll = long long;
using P = pair<int, int>;

vector<vector<int>> to;
vector<bool> visit;
int sum;
void dfs(int i){
  if(visit[i]) return;
  visit[i] = true;
  sum++;
  for(int t: to[i]) dfs(t);
  return;
}

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

  // //to全体を出力
  // rep(i,n){
  //   cout << "to" << i << " ";
  //   for(int t: to[i]) cout << t << " ";
  //   cout << endl;
  // }

  sum = 0;
  rep(i,n){
    visit = vector<bool>(n, false);
    dfs(i);
  }

  cout << sum << endl;
}