import Data.List
import Data.Bits

main = do
    [n, k] <- map read . words <$> getLine
    as <- map read . words <$> getLine
    let bestMask = calcBestMask k as
        in print . sum . map (\a -> a `xor` bestMask) $ as

-- number of elements with kth LSB set to 1
numBitSet :: Int -> [Int] -> Int
numBitSet _ [] = 0
numBitSet i (x:xs)
    | ithBit == 1 = 1 + numBitSet i xs
    | otherwise = numBitSet i xs
    where ithBit = shift x (-i) `mod` 2

topBit :: Int -> Int
topBit 0 = -1
topBit x = 1 + topBit (x `div` 2)

calcBestMask :: Int -> [Int] -> Int
calcBestMask k xs = calcBestMask' (topBit k) True k xs

calcBestMask' :: Int -> Bool -> Int -> [Int] -> Int
calcBestMask' (-1) _ _ _ = 0
calcBestMask' i b k xs
    | b && ithBit == 0 = calcBestMask' (i - 1) True k xs
    | b && ithBit == 1 = if numIthBitNotSet > numIthBitSet
        then shift 1 i + calcBestMask' (i - 1) True k xs
        else calcBestMask' (i - 1) False k xs
    | otherwise = if numIthBitNotSet > numIthBitSet
        then shift 1 i + calcBestMask' (i - 1) False k xs
        else calcBestMask' (i - 1) False k xs
    where
        ithBit = shift k (-i) `mod` 2
        numIthBitSet = numBitSet i xs
        numIthBitNotSet = length xs - numIthBitSet
