import sys input = sys.stdin.buffer.readline sys.setrecursionlimit(10 ** 7) N, M = map(int, input().split()) BC = tuple(tuple(map(int, input().split())) for _ in range(M)) val = set() for b, c in BC: val.add(b) val.add(c) val = sorted(val) vtoi = {v: i for i, v in enumerate(val)} sz = len(val) edge = [[] for _ in range(sz)] for b, c in BC: s = vtoi[b] t = vtoi[c] edge[s].append(t) vis = [0] * sz goal = list(range(sz)) def dfs(s, p): vis[s] = 1 for t in edge[s]: if vis[t]: goal[s] = max(goal[s], goal[t]) continue if t == p: continue dfs(t, s) goal[s] = max(goal[s], goal[t]) for i in range(sz): if vis[i]: continue dfs(i, -1) ans = N * (N + 1) // 2 for i, g in enumerate(goal): ans -= val[i] ans += val[g] print(ans)