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, val): vis[s] = 1 for t in edge[s]: if vis[t]: val = max(val, goal[t]) continue if t == p: continue dfs(t, s, val) val = max(val, goal[t]) goal[s] = max(goal[s], val) for i in range(sz): if vis[i]: continue dfs(i, -1, i) ans = N * (N + 1) // 2 for i, g in enumerate(goal): ans -= val[i] ans += val[g] print(ans)