open Printf
open Scanf

module L =
  struct
    include List
  
    let rec unfold f x =
      match f x with
        None -> []
      | Some (a, b) -> a :: unfold f b
  end
    
let () =
  let n = scanf "%d " (fun x -> x) in
  let k = scanf "%d " (fun x -> x) in
  let xs = L.unfold (fun b -> if b = 0 then None else let v = scanf "%d " (fun x -> x) in Some (v,b-1)) n in
  L.fold_left (fun s x -> s + 2 * (min x (k-x))) 0 xs |> printf "%d\n"
