l, n=gets.split(' ').map{|i| i.to_i}
array = []
for i in 1..n do
  array += [gets.to_i]
end
array.sort!
p = l
summ = 0

for j in 1..n do
  f = 0
  k = 0
  for i in 0..n-1 do 
  	r, lef =  [(array[i] - p > 0 ? array[i] - p : l+ array[i] - p),  (p-array[-1-i] > 0 ?  p-array[-1-i] : l+p-array[-1-i]) ]
    fr, flef = [r, lef] if i ==0
    #print [r, lef]
    #print [array[i] - p, l+ array[i] - p]
    #print [p-array[-1-i] , l+p-array[-1-i]]
  	if r > lef
      f  = 'r'
      k = fr
      break
    elsif lef > r
      f = 'l'
      k = flef
      break
    elsif i > n-1 /2
      f = 'r'
      k = fr
      break
    end
  end
  #print f
  #print k 
  #print '   '
  if f == 'r'
	p = array[0]
  elsif f =='l'
    p = array[-1]
  end
  array -= [p]
  summ += k
end

print summ
#print array