L,n=gets.split.map(&:to_i)
trees = []
n.times do
  trees.push(gets.to_i)
end

# 一旦再帰で計算してしまう。
def get_length current, trees, ret
  if trees.length == 0
    return ret
  else
    l_length = (current < trees[0]) ? trees[0] - current : trees[0] + (L - current)
    r_length = (current < trees[-1]) ? current + (L - trees[-1]) : current - trees[-1]
#    if l_length == r_length
      tmp_l_ret = ret + l_length
      tmp_l_current = trees[0]
      tmp_l_sum = get_length tmp_l_current, trees[1..-1], tmp_l_ret
      tmp_r_ret = ret + r_length
      tmp_r_current = trees[-1]
      tmp_r_sum = get_length tmp_r_current, trees[0...-1], tmp_r_ret
      (tmp_l_sum > tmp_r_sum) ? tmp_l_sum : tmp_r_sum
#    elsif l_length > r_length
#      ret += l_length
#      current = trees.shift
#      get_length current, trees, ret
#    else
#      ret += r_length
#      current = trees.pop
#      get_length current, trees, ret
#    end
  end
end

=begin
while trees.length > 0
  l_length = (current < trees[0]) ? trees[0] - current : trees[0] + (l - current)
  r_length = (current < trees[-1]) ? current + (l - trees[-1]) : current - trees[-1]
  if l_length > r_length
    ret += l_length
    current = trees.shift
  else
    ret += r_length
    current = trees.pop
  end
end
=end

puts get_length(0, trees, 0)
