import sys from collections import deque input = sys.stdin.buffer.readline N, M = map(int, input().split()) BC = [tuple(map(int, input().split())) for _ in range(M)] nodes = set() for b, c in BC: nodes.add(b) nodes.add(c) nodes = sorted(nodes) comp = {x: i for i, x in enumerate(nodes)} L = len(comp) G = [[] for _ in range(L)] for b, c in BC: G[comp[c]].append(comp[b]) table = nodes.copy() for i in range(L - 1, -1, -1): x = nodes[i] if x < table[i]: continue d = deque([i]) while d: v = d.pop() for u in G[v]: if table[u] < x: table[u] = x d.append(u) ans = N * (N + 1) // 2 - sum(nodes) + sum(table) print(ans)