#include"bits/stdc++.h" using namespace std; #define REP(k,m,n) for(int (k)=(m);(k)<(n);(k)++) #define rep(i,n) REP((i),0,(n)) using ll = long long; using pii = pair; using pll = pair; using tp3 = tuple; using Mat = vector>; constexpr int INF = 1 << 28; constexpr ll INFL = 1ll << 60; constexpr int dh[4] = { 0,1,0,-1 }; constexpr int dw[4] = { -1,0,1,0 }; bool isin(const int H, const int W, const int h, const int w) { return 0 <= h && h < H && 0 <= w && w < W; } // ============ template finished ============ int main() { ll N, M; cin >> N >> M; set vals; map> rev; rep(i, M) { ll B, C; cin >> B >> C; vals.insert(C); rev[C].push_back(B); } map id2mp; REP(val, 1, N + 1)id2mp[val] = val; { auto itr = vals.end(); do { --itr; for (auto next : rev[*itr]) { id2mp[next] = max(id2mp[next], id2mp[*itr]); } } while (itr != vals.begin()); } ll res = 0; REP(i, 1, N + 1) { res += id2mp.find(i) == id2mp.end() ? i : id2mp.at(i); } cout << res << endl; return 0; }