solve :: [Int] -> Int -> Int
solve [] _ = 0
solve (x:xs) k = r + solve xs k
  where r = (*2) $ min x (k - x)

main :: IO ()
main = do
  getLine
  k <- readLn
  xs  <- map read . words <$> getLine
  print $ solve xs k