open Batteries
let () =
  let n,k = Scanf.scanf "%d %d " (fun a b -> a,b) in
  let a_lst = Array.to_list @@ Array.init n (fun _ -> Scanf.scanf "%d " (fun a -> a)) in

  let minsum = List.fold_left (fun acc x -> acc + (2 * min (k-x) x)) 0 a_lst in
  
  Printf.printf "%d\n" minsum
