#line 1 "/workspaces/compro/lib/template.hpp"


#line 1 "/workspaces/compro/lib/io/vector.hpp"
#include <iostream>
#include <vector>

#ifndef IO_VECTOR
#define IO_VECTOR

template <class T> std::ostream &operator<<(std::ostream &out, const std::vector<T> &v) {
  int size = v.size();
  for (int i = 0; i < size; i++) {
    std::cout << v[i];
    if (i != size - 1)
      std::cout << " ";
  }
  return out;
}

template <class T> std::istream &operator>>(std::istream &in, std::vector<T> &v) {
  for (auto &el : v) {
    std::cin >> el;
  }
  return in;
}

#endif
#line 4 "/workspaces/compro/lib/template.hpp"
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) (v).begin(), (v).end()
#define coutd(n) cout << fixed << setprecision(n)
#define ll long long int
#define vl vector<ll>
#define vi vector<int>
#define MM << " " <<

using namespace std;

template <class T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}

template <class T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}

// 重複を消す。計算量はO(NlogN)
template <class T> void unique(std::vector<T> &v) {
  std::sort(v.begin(), v.end());
  v.erase(std::unique(v.begin(), v.end()), v.end());
}


#line 2 "main.cpp"

long long solve(int N, int M, const std::vector<int> &A, const std::vector<int> &B) {
  int ans = 0;
  vector<vi> g(N);
  REP(i, M) { g[A[i]].push_back(B[i]); }

  auto f = [&](auto self, int cur, vector<bool> &visited) -> void {
    visited[cur] = true;
    for (const auto v : g[cur]) {
      if (!visited[v]) {
        ans++;
        self(self, v, visited);
      }
    }
  };
  REP(i, N) {
    vector<bool> visited(N, false);
    f(f, i, visited);
  }
  return ans + N;
}

// generated by online-judge-template-generator v4.7.1 (https://github.com/online-judge-tools/template-generator)
int main() {
  int N, M;
  std::cin >> N >> M;
  std::vector<int> A(M), B(M);
  for (int i = 0; i < M; ++i) {
    std::cin >> A[i] >> B[i];
    A[i]--;
    B[i]--;
  }
  auto ans = solve(N, M, A, B);
  std::cout << ans << std::endl;
  return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ull unsigned long long int
const int mod=1e9+7;
const int mxx=1e5+10;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ff first
#define ss second
vector<int> adj[2222];
bool vis[2222];
int cnt;

void dfs(int u){
	vis[u]=1;
	for(auto v:adj[u]){
		if(!vis[v]){
			cnt++;
			dfs(v);
		}
	}
}

int main(){
	IOS;
	ll t=1,i;
	while(t--){
		int n,m;
		cin>>n>>m;
		
		for(i=0;i<m;i++){
			int u,v;
			cin>>u>>v;
			adj[u].push_back(v);
		}
		
		int ans=0;
		for(i=1;i<=n;i++){
			memset(vis,0,sizeof(vis));
			cnt=1;
			dfs(i);
			ans+=cnt;
		}
	
		cout<<ans;
	}
}