import Debug.Trace

main :: IO ()
main = do
    getLine
    k <- readLn :: IO Int
    xs <- map (read :: String -> Int) . words <$> getLine
    putStr $ show (solve k xs) ++ "\n"

solve k [] = 0
solve k (x:xs) = (min x (k - x)) * 2 + solve k xs
