#define _USE_MATH_DEFINES #include using namespace std; #define FOR(i,m,n) for(int i=(m);i<(n);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) (v).begin(),(v).end() using ll = long long; const int INF = 0x3f3f3f3f; const ll LINF = 0x3f3f3f3f3f3f3f3fLL; const double EPS = 1e-8; const int MOD = 1000000007; // const int MOD = 998244353; const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1}; const int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1}; template inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; } template inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; } struct IOSetup { IOSetup() { cin.tie(nullptr); ios_base::sync_with_stdio(false); cout << fixed << setprecision(20); } } iosetup; using CostType = bool; struct Edge { int src, dst; CostType cost; Edge(int src, int dst, CostType cost = 0) : src(src), dst(dst), cost(cost) {} inline bool operator<(const Edge &x) const { return cost != x.cost ? cost < x.cost : dst != x.dst ? dst < x.dst : src < x.src; } inline bool operator<=(const Edge &x) const { return !(x < *this); } inline bool operator>(const Edge &x) const { return x < *this; } inline bool operator>=(const Edge &x) const { return !(*this < x); } }; struct SCC { vector id; vector> vertices; vector> comp; SCC(const vector> &graph, bool heavy = false) : graph(graph), heavy(heavy) { n = graph.size(); rev_graph.resize(n); REP(i, n) for (const Edge &e : graph[i]) { rev_graph[e.dst].emplace_back(e.dst, e.src, e.cost); } used.assign(n, false); id.assign(n, -1); REP(i, n) { if (!used[i]) dfs(i); } int now = 0; for (int i = n - 1; i >= 0; --i) { if (id[order[i]] == -1) { if (heavy) vertices.emplace_back(); rev_dfs(order[i], now++); } } comp.resize(now); REP(i, n) for (const Edge &e : graph[i]) { if (id[i] != id[e.dst]) comp[id[i]].emplace_back(id[i], id[e.dst], e.cost); } // if (heavy) { // REP(i, now) sort(ALL(vertices[i])); // } } private: bool heavy; int n; vector> graph, rev_graph; vector used; vector order; void dfs(int ver) { used[ver] = true; for (const Edge &e : graph[ver]) { if (!used[e.dst]) dfs(e.dst); } order.emplace_back(ver); } void rev_dfs(int ver, int now) { id[ver] = now; if (heavy) vertices[now].emplace_back(ver); for (Edge &e : rev_graph[ver]) { if (id[e.dst] == -1) rev_dfs(e.dst, now); } } }; vector topological_sort(const vector> &graph) { int n = graph.size(); vector deg(n, 0); REP(i, n) { for (const Edge &e : graph[i]) ++deg[e.dst]; } queue que; REP(i, n) { if (deg[i] == 0) que.emplace(i); } vector res; while (!que.empty()) { int ver = que.front(); que.pop(); res.emplace_back(ver); for (const Edge &e : graph[ver]) { if (--deg[e.dst] == 0) que.emplace(e.dst); } } return res.size() == n ? res : vector(); } int main() { int n, m; cin >> n >> m; vector> graph; map mp; vector rev; while (m--) { int b, c; cin >> b >> c; if (mp.count(b) == 0) { graph.emplace_back(); mp[b] = rev.size(); rev.emplace_back(b); } if (mp.count(c) == 0) { graph.emplace_back(); mp[c] = rev.size(); rev.emplace_back(c); } graph[mp[b]].emplace_back(mp[b], mp[c]); } SCC scc(graph, true); int l = scc.comp.size(); vector val(l, 0); REP(i, l) { for (int e : scc.vertices[i]) chmax(val[i], rev[e]); } // REP(i, l) cout << val[i] << " \n"[i + 1 == l]; vector t = topological_sort(scc.comp); reverse(ALL(t)); for (int v : t) for (const Edge &e : scc.comp[v]) chmax(val[v], val[e.dst]); // REP(i, l) cout << val[i] << " \n"[i + 1 == l]; ll ans = 1LL * n * (n + 1) / 2; REP(i, l) for (int e : scc.vertices[i]) { ans -= rev[e]; ans += max(rev[e], val[i]); } cout << ans << '\n'; return 0; }