import Control.Arrow
import Control.Monad
import Data.List
import qualified Data.ByteString.Char8 as B
import Data.Bits
import qualified Data.Vector as V
import Debug.Trace

main :: IO ()
main = getLine >> readContents 2 >>= print . solve 

solve :: [[Int]] -> Int
solve [as, bs] = fst $ foldl' ff (0, (as, sort bs)) [27,26..0]
    where
        ff (acc, (xs, ys)) e
            | odd res = (acc + v, next)
            | otherwise = (acc, next)
            where
                next = (map sf xs, sort $ map sf ys)
                sf = (`mod` (shift 1 e))
                res = foldl' count 0 xs
                v = shiftL 1 e
                vs = V.fromList ys
                count acc a = acc + max 0 (l1 - l0) + max 0 (l3 - l2)
                    where
                        [l0, l1, l2, l3] = map mf [1..4]
                        mf x = either id succ $ binarySearch (x * v - a - 1) vs

binarySearch :: (Ord a) => a -> V.Vector a -> Either Int Int
binarySearch k xs
    | V.null xs = Left 0
    | otherwise = bsf 0 $ V.length xs
    where
        bsf base size
            | size > 1 && mv > k = bsf base $ size - half
            | size > 1 = bsf mid $ size - half
            | bv == k = Right base
            | bv < k = Left $ base + 1
            | otherwise = Left base
            where
                half = size `div` 2
                mid = base + half
                bv = xs V.! base
                mv = xs V.! mid

readLine :: IO [Int]
readLine = unfoldr (\s -> second (maybe B.empty snd . B.uncons) <$> B.readInt s) <$> B.getLine

readContents :: Int -> IO [[Int]]
readContents = flip replicateM readLine