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

/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

template <typename T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define oset_find(k) find_by_order(k)
#define oset_order(k) order_of_key(k)*/

using ll = long long;
using db = long double;
using vi = vector<int>;
using vl = vector<ll>;
using pi = pair<int, int>;
using pl = pair<ll, ll>;

template<typename T> using pqg = priority_queue<T, vector<T>, greater<T> >;

#define FOR(i, a, n) for(int i=(a); i<(n); ++i)
#define ROF(i, a, n) for(int i=(n)-1; i>=(a); --i)

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) int((x).size())
#define ins insert
#define pb push_back
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define endl '\n'

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

template<typename T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<typename T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }

#define ts to_string
string ts(char c) { return string(1, c); }
string ts(const char* s) { return (string)s; }
string ts(const string& s) { return '"' + s + '"'; }
string ts(bool b) { return (b ? "true" : "false"); }

string ts(vector<bool> v) {
    string res = "{";
    FOR(i, 0, sz(v))
        res += char('0' + v[i]);
    res += "}";
    return res;
}

template <size_t SZ> string ts(bitset<SZ> b) {
    string res;
    FOR(i, 0, SZ)
        res += char('0' + b[i]);
    return res;
}

template <typename A, typename B> string ts(pair<A, B> p);
template <typename A> string ts(A v) {
    bool first = 1; string res = "{";
    for (const auto& x: v) {
        if (!first) res += ", ";
        first = 0; res += ts(x);
    }
    res += "}"; return res;
}

template <typename A, typename B> string ts(pair<A, B> p) {
    return "(" + ts(p.fi) + ", " + ts(p.se) + ")";
}

void dbgOut() { cerr << "]" << endl; }
template <typename H, typename... T> void dbgOut(H h, T... t) {
    cerr << ts(h); if (sizeof...(t)) cerr << ", ";
    dbgOut(t...);
}

#ifdef LOCAL
    #define dbg(...) cerr << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", dbgOut(__VA_ARGS__)
#else
    #define dbg(...) 0
#endif

const int MOD = 1e9+7; // 998244353;
const ll INF = 1e18;
const int MX = 2e5+5;

vi adj[2005];
bool vis[2005];

int dfs(int i) {
    int cnt = 1;

    vis[i] = 1;
    for (auto x : adj[i]) {
        if (!vis[x])
            cnt += dfs(x);
    }
    return cnt;
}

void solve() {
    int n, m; cin >> n >> m;

    while (m--) {
        int x, y; cin >> x >> y;
        --x; --y;
        adj[x].pb(y);
    }

    ll ans = 0;
    FOR(i, 0, n) {
        ans += dfs(i);

        fill_n(vis, 2005, 0);
        dbg(ans);
    }

    cout << ans << endl;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

#ifdef LOCAL
    auto begin = std::chrono::high_resolution_clock::now();
#endif

    solve();

#ifdef LOCAL
    auto end = std::chrono::high_resolution_clock::now();
    cerr << "\nExecution time: " << std::chrono::duration_cast<std::chrono::duration<double>> (end-begin).count() << " seconds\n";
#endif
}

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

#define FAST ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define ll long long
#define all(x) (x).begin(), (x).end()
#define ff first
#define ss second

void input(int *arr, int n){
	for(int i=0; i<n; ++i) cin >> arr[i];
}

void input(vector<int>&arr, int n){
	for(int i=0; i<n; ++i) cin >> arr[i];
}

void print(int *arr, int n){
	for(int i=0; i<n; ++i) cout << arr[i] << " ";
	cout << "\n";
}

void print(vector<int>&arr, int n){
	for(int i=0; i<n; ++i) cout << arr[i] << " ";
	cout << "\n";
}

vector<vector<int> >vec;
vector<bool>visited;
ll ans = 0;

void dfs(int node){
	visited[node] = true;
	for(int to : vec[node]){
		if(visited[to])
			continue;
		else{
			ans += 1;
			//cout << ans << " " << node << " " << to << "\n";
			dfs(to);
		}
	}
}

int main(){
	FAST;
	int test = 1;
	//cin >> test;
	for(int tt=1; tt<=test; ++tt){
		int n, m;
		cin >> n >> m;
		vec = vector<vector<int> >(n+1, vector<int>());
		for(int i=1; i<=m; ++i){
			int u, v;
			cin >> u >> v;
			vec[u].push_back(v);
		}
		for(int i=1; i<=n; ++i){
			visited = vector<bool>(n+1, false);
			//cout << i << "\n";
			dfs(i);
		}
		cout << ans+n << "\n";
	}
	return 0;
}