N, M = gets.split.map(&:to_i) nums = [] E = Hash.new { |h, k| h[k] = [] } M.times do b, c = gets.split.map(&:to_i) nums << b nums << c E[c] << b end nums.uniq! nums.sort! visited = Hash.new(false) memo = Hash.new nums.reverse.each do |root| next if visited[root] queue = [] queue << root until queue.empty? v = queue.shift next if visited[v] visited[v] = true memo[v] = root E[v].each do |u| next if u > root queue << u end end end sum = N * (1 + N) / 2 nums.each do |n| if memo[n] sum += memo[n] - n end end puts sum