import sys input = sys.stdin.readline from collections import * def bfs(s): if ma[s]!=-1: return q = deque([s]) ma[s] = s while q: v = q.popleft() for nv in G[v]: if ma[nv]==-1: ma[nv] = s q.append(nv) N, M = map(int, input().split()) BC = [tuple(map(int, input().split())) for _ in range(M)] l = [] for B, C in BC: l.append(B-1) l.append(C-1) l = list(set(l)) l.sort() idx = defaultdict(int) for i in range(len(l)): idx[l[i]] = i G = [[] for _ in range(len(l))] for B, C in BC: G[idx[C-1]].append(idx[B-1]) ma = [-1]*len(l) for i in range(len(l)-1, -1, -1): bfs(i) ans = N*(N+1)//2 for li in l: ans -= li ans += l[ma[idx[li]]] print(ans)