#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define mydeb(fmt, ...) fprintf(stderr, fmt, __VA_ARGS__)
//#define mydeb(fmt, ...) 


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

    vector<vector<int>> g(n);
    rep(i,m)
    {
        g[a[i]].push_back(b[i]);
    }

    int total=0;
    rep(i,n)
    {
        int count=0;
        vector<bool> visited(n);
        queue<int> q;

        q.push(i);
        while (q.empty()==false)
        {
            int pp=q.front();
            q.pop();
            if (visited[pp]!=false)
            {
                continue;
            }

            visited[pp]=true;
            ++count;
            for (auto nn: g[pp])
            {
                if (visited[nn]==false)
                {
                    q.push(nn);
                }
            }
        }
        total+=count;
    }
    cout << total << endl;
}



#include<bits/stdc++.h>
using namespace std;

#define INF  						1e15
#define EPS   						1e-9
#define MOD 						1000000007
#define Pi 							3.14159265358979323846264338327950288419716939937510

#define umap 						unordered_map
#define uset 						unordered_set
#define fi 							first
#define se 							second
#define ll 							long long
#define vi 							vector<int>
#define pii 						pair<int,int>
#define vll 						vector<ll>
#define pll							pair<ll,ll>
#define vvll						vector<vector<ll>>
#define pb 							push_back
#define pob 						pop_back
#define pf 							push_front
#define pof 						pop_front
#define maxE(a,n) 					*max_element(a,a+n);
#define minE(a,n) 					*min_element(a,a+n);
#define all(x) 						x.begin(), x.end()
#define rev(x)						reverse(all(x))
#define endl						"\n"

#define fast 						ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)

#define rep(i,n)					for (ll  i=0;i<n;i++)
#define repd(i,n)					for (ll  i = n-1;i>=0;i--)
#define fori(i,a,n) 				for (ll  i=a;i<n;i++)
#define ford(i,a,n) 				for (ll  i=n-1;i>=a;i--)
#define trav(x,a)					for(auto x:a){cout<<x<<" ";}cout<<endl;

ll gcd(ll a, ll b) {	return __gcd(a, b);}
ll lcm(ll a, ll b) {return (a * b) / gcd(a, b);}
ll power(ll x, ll b, ll m) {ll p = 1; while (b > 0) {if (b & 1) {p = p * x; p %= m;} b >>= 1; x *= x; x %= m;} return p % m;}
void dbg(string s) {cout << s << endl;}


/*PBDS*/
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;

typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> PBDS;

// pbds.find_by_order(k) (return iterator to kth largest element(from 0))
// pbds.order_of_key(k) (return number of elements in set which are strictly less than k)

vector<vector<ll>> g;
vector<bool> vis;
vector<ll> dp;
ll cnt;

void dfs(ll i, ll parent) {
	if (i != parent)
		cnt++;
	vis[i] = true;

	for (auto nbr : g[i]) {
		if (vis[nbr] != true) {
			dfs(nbr, i);
		}
	}

	return;
}


void solve() {

	ll n, m;
	cin >> n >> m;

	g = vector<vector<ll>>(n + 1);

	dp = vector<ll>(n + 1, 0);
	cnt = 0;
	rep(i, m) {
		ll u, v;
		cin >> u >> v;

		g[u].pb(v);
	}

	fori(i, 1, n + 1) {
		vis = vector<bool>(n + 1, false);
		dfs(i, 0);
	}

	cout << cnt << endl;
}


int main() {

#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	fast;
	ll t = 1;
	while (t--) {
		solve();
	}
}