total_cost = 0
max_cost = 0
n,m = gets.split.map(&:to_i)
m.times do |i|
    a,b = gets.split.map(&:to_i)
    if a < n then
        cost = n - a
        total_cost += cost
        max_cost = cost if max_cost < cost
    end
end
puts total_cost - max_cost
