import sys 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 = table[i] for j in G[i]: if table[j] < x: table[j] = x ans = N * (N + 1) // 2 - sum(nodes) + sum(table) print(ans)