#include #include #include template struct Edge { int src, dst; Cost cost; Edge(int src = -1, int dst = -1, Cost cost = 1) : src(src), dst(dst), cost(cost){}; bool operator<(const Edge& e) const { return this->cost < e.cost; } bool operator>(const Edge& e) const { return this->cost > e.cost; } }; template using Edges = std::vector>; template struct Graph { std::vector>> graph; Graph(int n = 0) : graph(n) {} void span(bool direct, int src, int dst, Cost cost = 1) { graph[src].emplace_back(src, dst, cost); if (!direct) graph[dst].emplace_back(dst, src, cost); } int size() const { return graph.size(); } void clear() { graph.clear(); } void resize(int n) { graph.resize(n); } std::vector>& operator[](int v) { return graph[v]; } std::vector> operator[](int v) const { return graph[v]; } }; using lint = long long; void solve() { int n, m; std::cin >> n >> m; Graph<> graph(n); while (m--) { int u, v; std::cin >> u >> v; graph.span(true, --v, --u); } std::vector to(n, -1); for (int x = n - 1; x >= 0; --x) { if (to[x] != -1) continue; to[x] = x; std::queue que; que.push(x); while (!que.empty()) { int v = que.front(); que.pop(); for (auto e : graph[v]) { int u = e.dst; if (to[u] != -1) continue; to[u] = x; que.push(u); } } } lint ans = 0; for (auto x : to) ans += x + 1; std::cout << ans << "\n"; } int main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); solve(); return 0; }