(L, N) = gets.split.map(&:to_i)
X = []
N.times do
  X << gets.to_i
end

current = 0
sum = 0
until X.size.zero?
  # 反時計回り距離
  ccw = X.first - current
  ccw = L + ccw if ccw < 0
  # 時計回り距離
  cw = current - X.last
  cw = L + cw if cw < 0

  if ccw > cw
    sum += ccw
    current += ccw
    current = current - L if current >= L
    X.shift
  else
    sum += cw
    current -= cw
    current = L + current if current < 0
    X.pop
  end
end

p sum