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

int main() {

	ios::sync_with_stdio(false);
	cin.tie(nullptr);

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

	int n, m;
	cin >> n >> m;
	vector<vector<int>> g(n);
	for (int i = 0; i < m; i++) {
		int a, b; cin >> a >> b;
		--a; --b; g[a].push_back(b);
	}

	int ans = 0;
	vector<int> vis(n, 0);
	auto dfs = [&](int b, const auto & self) -> int {
		int c = 1;
		vis[b] = 1;
		for (auto i : g[b]) {
			if (!vis[i]) {
				c += self(i, self);
			}
		}
		return c;
	};

	for (int i = 0; i < n; i++) {
		vis.assign(n, 0);
		ans += dfs(i, dfs);
	}

	cout << ans << "\n";

	return 0;
}
#include <iostream>
#include <bitset>
#define REP(i, n) for (int i = 0; i < (n); i++)

std::bitset<2000>   d[2000];
int n, m, a, b, res;

int main() {
    std::cin >> n >> m;
    REP(i, n)
        d[i][i] = 1;
    REP(i, m) {
        std::cin >> a >> b;
        d[--a][--b] = 1;
    }
    REP(i, n) REP(j, n) if (d[j][i])
        d[j] |= d[i];
    REP(i, n)
        res += d[i].count();
    std::cout << res << std::endl;
    return 0;
}
