/*
* Author: JophieQu
* Date: 06/06/21 19:53
* Welcome to follow my sister's CODEFORCES account:@Cccc23333
*/
#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
const int N=2e3+7;
const int mod=1e9+7;
using namespace std;
ll ans=0;
vector<ll>e[N+1];
int vis[N];
void dfs(int ii,int fa,int f){
	if(ii==fa&&f) return ;
	for(int i=0;i<e[ii].size();++i){
		if(!vis[e[ii][i]]){
			vis[e[ii][i]]=1;
			++ans;
		//	cout<<fa<<" "<<ii<<" "<<e[ii][i]<<" "<<ans<<endl;
			dfs(e[ii][i],fa,1);
		}
	}
}
void solves(){
	int n,m;cin>>n>>m;
	while(m--){
		int a,b;cin>>a>>b;
		e[a].push_back(b);
	}
	for(int i=1;i<=n;++i){
		memset(vis,0,sizeof(vis));
		vis[i]=1;
		dfs(i,i,0);
	}
	cout<<ans+n<<endl;
}
int main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int OwwwO=1;
//	cin>>OwwwO;
	while(OwwwO--){
		solves();
	}
}


#include <bits/stdc++.h>
#include <cassert>
#define rep(i, N) for (int i = 0; i < (N); ++i)
#define rep2(i, a, b) for (ll i = a; i <= b; ++i)
#define rep3(i, a, b) for (ll i = a; i >= b; --i)
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define nl '\n'
#define endl '\n'
#define all(c) begin(c), end(c)
#define ok() puts(ok ? "Yes" : "No");
#define pcnt(x) __builtin_popcountll(x)
#define df(x) ll x = in();
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
using namespace std;
using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using vvll = vector<vll>;
using ii = pair<int, int>;
using vvi = vector<vi>;
using vii = vector<ii>;
using vs = vector<string>;
using P = pair<ll, ll>;
using gt = greater<P>;
template <class T> using minq = priority_queue<T, vector<T>, greater<T>>;
using vP = vector<P>;
inline ll in() {
  ll x;
  scanf("%lld", &x);
  return x;
}
template <class T> void takeUnique(vector<T> &v) {
  auto last = std::unique(v.begin(), v.end());
  v.erase(last, v.end());
}
template <class T> void print(const initializer_list<T> &il) {
  for (auto x : il) {
    cout << x << " ";
  }
  cout << "\n";
}
inline void priv(vll a) {
  rep(i, (int)a.size() - 1) cout << a[i] << " ";
  cout << a.back() << nl;
}
inline void priv(vi a) {
  rep(i, (int)a.size() - 1) cout << a[i] << " ";
  cout << a.back() << nl;
}
const ll LINF = 8e18L + 1;
const int INF = 8e9 + 1;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int dxx[] = {0, 1, 1, 1, 0, -1, -1, -1};
int dyy[] = {1, 1, 0, -1, -1, -1, 0, 1};
// g++ -std=c++17 -stdlib=libc++
#define _GLIBCXX_DEBUG
// This slows down the execution; even the time complexity, since it checks if
// std funcs such as lower_bound meets prereqs
vi to[2005];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout << fixed << setprecision(16);
  int n, m;
  cin >> n >> m;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    to[a].push_back(b);
  }
  int ans = 0;
  auto f = [&](int v) {
    vi vis(n);
    vis[v] = 1;
    queue<int> q;
    q.push(v);
    while (q.size()) {
      int u = q.front();
      q.pop();
      for (int nx : to[u]) {
        if (vis[nx])
          continue;
        q.push(nx);
      }
      vis[u] = true;
    }
    rep(i, n) {
      if (vis[i])
        ans++;
    }
  };
  rep(i, n) { f(i); }
  cout << ans << nl;

  return 0;
}
