## https://yukicoder.me/problems/no/1816 from collections import deque def main(): N, M = map(int, input().split()) bc = [] for _ in range(M): b, c = map(int, input().split()) bc.append((b, c)) # 数の集合を計算 number_set = set() for b, c in bc: number_set.add(b) number_set.add(c) number_list = list(number_set) number_list.sort(reverse=True) # グラフを構築 next_nodes = {} for b, c in bc: if c not in next_nodes: next_nodes[c] = [] next_nodes[c].append(b) max_numbers = {} for n in number_list: if n in max_numbers: continue max_numbers[n] = n queue = deque() queue.append(n) while len(queue) > 0: v = queue.popleft() if v in next_nodes: for w in next_nodes[v]: if w in max_numbers: continue max_numbers[w] = n queue.append(w) answer = (N * (N + 1)) // 2 for n, v in max_numbers.items(): answer += -n + v print(answer) if __name__ == "__main__": main()