n, m = map(int, input().split()) max_map = {} for _ in range(m): b, c = map(int, input().split()) if c > b: if b not in max_map or c > max_map[b]: max_map[b] = c cache = {} def get_final(i): if i not in max_map: return i if i in cache: return cache[i] path = [] current = i while current in max_map: if current in cache: final = cache[current] break path.append(current) current = max_map[current] else: final = current # Update cache for all nodes in the path for node in path: cache[node] = final return final total = n * (n + 1) // 2 for b in max_map: final = get_final(b) total += (final - b) print(total)