let memoize n f =
  let dp = Hashtbl.create n in
  let rec get x =
    try Hashtbl.find dp x with
    | Not_found ->
        let result = f get x in
        Hashtbl.add dp x result;
        result in
  get

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 = memoize (2 * n * n) @@ fun dp (l, r, b) ->
    if r <= l then 0
    else if b then max (xs.(l + 1) - xs.(l) + dp (l + 1, r, true)) (len - xs.(r) + xs.(l) + dp (l + 1, r, false))
    else max (xs.(r) - xs.(r - 1) + dp (l, r - 1, false)) (len - xs.(r) + xs.(l) + dp (l, r - 1, true))
  in
  Printf.printf "%d\n" @@ max (xs.(0) + dp (0, n - 1, true)) (len - xs.(n - 1) + dp (0, n - 1, false))

