import sys from collections import defaultdict def main(): N, M = map(int, sys.stdin.readline().split()) operations = [] for _ in range(M): B, C = map(int, sys.stdin.readline().split()) if C > B: operations.append((B, C)) S = set() edges = defaultdict(list) for B, C in operations: edges[B].append(C) S.add(B) S.add(C) sorted_S = sorted(S, reverse=True) max_reach = {x: x for x in S} for x in sorted_S: if x in edges: max_val = 0 for y in edges[x]: if max_reach[y] > max_val: max_val = max_reach[y] max_reach[x] = max(x, max_val) sum_S_contribution = sum(max_reach[x] for x in S) sum_in_S = sum(S) sum_total = N * (N + 1) // 2 sum_out_S = sum_total - sum_in_S total = sum_S_contribution + sum_out_S print(total) if __name__ == "__main__": main()