N, M = map(int, input().split()) max_replace = {} for _ in range(M): B, C = map(int, input().split()) if C > B: if B in max_replace: if C > max_replace[B]: max_replace[B] = C else: max_replace[B] = C cache = {} def get_max(x): if x not in max_replace: return x if x in cache: return cache[x] path = [] current = x while current in max_replace and current not in cache: path.append(current) current = max_replace[current] if current in cache: res = cache[current] else: res = current for node in path: cache[node] = res cache[x] = res return res total_increment = 0 for B in max_replace: max_val = get_max(B) total_increment += (max_val - B) initial_sum = N * (N + 1) // 2 result = initial_sum + total_increment print(result)