open Batteries

let read_two () = Scanf.sscanf (read_line ()) "%d %d" (fun a b -> (a, b))

let read_three () =
  Scanf.sscanf (read_line ()) "%d %d %d" (fun a b c -> (a, b, c))

let read_four () =
  Scanf.sscanf (read_line ()) "%d %d %d" (fun a b c d -> (a, b, c, d))

(* a**x *)
let pow a x =
  let rec aux acc = function 0 -> acc | n -> aux (acc * a) (n - 1) in
  aux 1 x

let rec f k acc = function
  | [] -> acc
  | x :: rest ->
      let d = min x (abs (k - x)) in
      f k (acc + (d * 2)) rest

let () =
  let _ = read_int () and k = read_int () in
  let xs =
    read_line () |> String.split_on_char ' '
    |> List.map (fun s -> int_of_string s)
  in
  print_int (f k 0 xs);
  print_newline ()
