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)
  if x.size == 1
    [diff(p, x[0]), diff(x[0], p)].max
  else
    idx = x.bsearch_index { |e| e > p }
    idx = 0 if idx.nil?
    [diff(x[idx], p) + dfs(x - [x[idx]], x[idx]),
     diff(p, x[idx - 1]) + dfs(x - [x[idx - 1]], x[idx - 1])].max
  end
end
puts dfs(xs, 0)
