#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; using ull = unsigned long long; template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } template T gcd(T a, T b) { a = abs(a), b = abs(b); while (b > 0) { tie(a, b) = make_pair(b, a % b); } return a; } //mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); constexpr long long INF = 1LL << 60; constexpr int inf = 1000000007; //constexpr long long mod = 1000000007LL; constexpr long long mod = 998244353; constexpr int MAX = 1100000; template struct Compress { vector v; Compress() {} Compress(vector _v) :v(_v) { build(); } void add(T x) { v.emplace_back(x); } void build() { sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); } void build(vector _v) { v = _v; sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); } int get(T x) { return lower_bound(v.begin(), v.end(), x) - v.begin(); } T& operator[](int i) { return v[i]; } int size() { return (int)v.size(); } }; struct StronglyConnectedComponents { int n, group_num, now_ord; vector> g; vector low, ord, idx; StronglyConnectedComponents(int _n) :n(_n), low(n), ord(n, -1), idx(n), g(n), group_num(0), now_ord(0) {} void add_edge(int from, int to) { g[from].emplace_back(to); } void dfs(int cur, vector& visited) { ord[cur] = low[cur] = now_ord++; visited.emplace_back(cur); for (auto nxt : g[cur]) { if (ord[nxt] == -1) { dfs(nxt, visited); chmin(low[cur], low[nxt]); } else { chmin(low[cur], ord[nxt]); } } if (ord[cur] == low[cur]) { while (true) { int v = visited.back(); visited.pop_back(); ord[v] = n; //別のdfs木からこないように idx[v] = group_num; if (cur == v) break; } group_num++; } } //sccのidxに入ってる頂点番号 idxはトポロジカル順 void build() { vector visited; for (int i = 0; i < n; i++) if (ord[i] == -1) dfs(i, visited); for (int i = 0; i < n; i++) idx[i] = group_num - 1 - idx[i]; } //build()の後にやる vector> groups() { vector> ret(group_num); for (int i = 0; i < n; i++) { ret[idx[i]].emplace_back(i); } return ret; } int operator[](int i) { return idx[i]; } }; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); ll n, m; cin >> n >> m; vector> vp(m); Compress comp; for (int i = 0; i < m; i++) { ll b, c; cin >> b >> c; vp[i] = { b,c }; comp.add(b); comp.add(c); } comp.build(); StronglyConnectedComponents scc(comp.size()); for (auto p : vp) { scc.add_edge(comp.get(p.first), comp.get(p.second)); } scc.build(); auto group = scc.groups(); vector mx(group.size()); auto g = scc.g; for (int i = 0; i < group.size(); i++) { for (auto& e : group[i]) { chmax(mx[i], comp[e]); } } for (int i = (int)group.size() - 1; i >= 0; i--) { for (auto cur : group[i]) { for (auto nxt : g[cur]) { chmax(mx[i], mx[scc[nxt]]); } } } ll res = n * (n + 1) / 2; for (int i = 0; i < group.size(); i++) { for (auto& e : group[i]) { res += mx[i] - comp[e]; } } cout << res << endl; }