
let () = Scanf.scanf "%d %d\n" @@ fun len n ->
  let xs = Array.init n @@ fun _ -> Scanf.scanf "%d\n" @@ fun x -> x in
  let dp = Array.init 2 @@ fun _ -> Array.make_matrix n n 0 in
  for w = 1 to n - 1 do
    for l = 0 to n - 1 - w do
      dp.(0).(l).(l + w) <- max (xs.(l + 1) - xs.(l) + dp.(0).(l + 1).(l + w)) (len - xs.(l + w) + xs.(l) + dp.(1).(l + 1).(l + w));
      dp.(1).(l).(l + w) <- max (xs.(l + w) - xs.(l + w - 1) + dp.(1).(l).(l + w - 1)) (len - xs.(l + w) + xs.(l) + dp.(0).(l).(l + w - 1))
    done
  done;
  Printf.printf "%d\n" @@ 
    max (xs.(0) + dp.(0).(0).(n - 1)) (len - xs.(n - 1) + dp.(1).(0).(n - 1))

