input = STDIN.read.split("\n").map{|l| l.split(" ").map{|a| a.to_i}}
first = input.shift

L = first[0]
N = first[1]
points = input.map{|r| r[0]}
COSTS = []
DIC = {}

def burn(trees, cost, current_pt)
  k = trees.to_s + '|' + cost.to_s + '|' + current_pt.to_s

  if DIC.key?(k)
    return cost
  else
    DIC[k] = cost
  end

  if trees.length === 0 
    return cost
  end

  dtf = nil
  f_trees = trees.clone
  tf = f_trees.shift
  if tf > current_pt
    dtf = tf - current_pt
  else
    dtf = tf + (L - current_pt)
  end
  fc = burn(f_trees, cost + dtf, tf)

  dtb = nil
  b_trees = trees.clone
  tb = b_trees.pop
  if tb < current_pt
    dtb = current_pt - tb
  else
    dtb = current_pt + (L - tb)
  end
  bc = burn(b_trees, cost + dtb, tb)

  res = nil
  if fc > bc
    res = fc
  else
    res = bc
  end
  return res
end

cost = burn(points, 0, 0)

puts cost