
#include <iostream>
#include <iomanip>
#include <vector>
#include <array>
#include <stack>
#include <queue>
#include <tuple>
#include <algorithm>
#include <unordered_set>
#include <unordered_map>
#include <cmath>
#include <map>
#include <numeric>
#include <cassert>
#include <string>
#include <list>

using ull = unsigned long long;
using ll = long long;

#define REP(i, n) for(ll i=0;i<(ll)n;i++)
#define REPab(i,a,b)for(ll i=(ll)a;i<(ll)b;i++)
constexpr bool bitp(ull bits, ull pos) { return bits & (1ULL << pos); }
constexpr ll mod(ll val, ll m) { return (val >= 0) ? (val % m) : (val % m + m); }
template <class T> void chmin(T& a, T b) { a = std::min(a, b); }
template <class T> void chmax(T& a, T b) { a = std::max(a, b); }
ll extgcd(const ll a, const ll b, ll& x, ll& y) {ll d = a;if (b != 0) { d = extgcd(b, a % b, y, x);y -= (a / b) * x;} else { x = 1; y = 0; } return d;}

int main() {
    ll N, M;
    std::cin >> N >> M;
    std::vector<std::vector<ll>> edges(N);
    REP(i, M) {
        ll a, b;
        std::cin >> a >> b;
        edges[a - 1].push_back(b - 1);
    }
    ll sum = 0;
    REP(i, N) {
        std::vector<bool> visited(N, false);
        const auto search = [&](auto&& search, ll node) -> void {
            for (const auto& edge : edges[node]) {
                if (!visited[edge]) {
                    visited[edge] = true;
                    sum++;
                    search(search, edge);
                }
            }
        };
        visited[i] = true;
        sum++;
        search(search, i);
    }
    std::cout << sum << std::endl;
}

#include <bits/stdc++.h>
using namespace std;
const int MAXN=2000;
vector<vector<int>> g;
bool temp[MAXN];
void dfs(int v){
	if(temp[v]) return;
	temp[v]=1;
	for(auto vv:g[v]) dfs(vv);
	return;
}
int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	int n,m;
	cin>>n>>m;
	g.resize(n);
	for(int i=0;i<m;i++){
		int a,b;
		cin>>a>>b;
		g[a-1].push_back(b-1);
	}
	int ans=0;
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++) temp[j]=0;
		dfs(i);
		for(int j=0;j<n;j++)
		if(temp[j]) ans++;
	}
	cout<<ans;
	return 0;
}
