#define _USE_MATH_DEFINES #include using namespace std; map> g; map vis; map to; int dfs (int cur) { if (vis[cur]) return to[cur]; vis[cur] = true; int res = cur; for (int& nxt : g[cur]) { res = max(res, dfs(nxt)); } return to[cur] = res; } signed main() { ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; g[a].push_back(b); } long long ans = 1LL * n * (n + 1) / 2; for (auto& p : g) { int get = dfs(p.first); ans += get - p.first; } cout << ans << endl; return 0; }