L, n = gets.split.map(&:to_i)
xs = []
n.times do
  xs.push gets.chomp.to_i
end
def diff(a, b)
  (a - b) % L
end
def dfs(x, p)
  return 0 if x.nil?
  if x.size == 1
    [diff(p, x[0]), diff(x[0], p)].max
  else
    [diff(x[0], p) + dfs(x[1..-1], x[0]),
     diff(p, x[-1]) + dfs(x[0..-2], x[-1])].max
  end
end
puts dfs(xs, 0)
