N, M = map(int, input().split()) replace_map = {} for _ in range(M): B, C = map(int, input().split()) if C > B: if B in replace_map: if C > replace_map[B]: replace_map[B] = C else: replace_map[B] = C cache = {} sum_gain = 0 for B in replace_map: current = B path = [] while True: if current in cache: value = cache[current] break if current not in replace_map: value = current break path.append(current) current = replace_map[current] # Update cache for all nodes in the path for node in path: cache[node] = value sum_gain += (value - B) initial_sum = N * (N + 1) // 2 total = initial_sum + sum_gain print(total)