import Control.Monad
import Control.Monad.ST
import Data.List
import qualified Data.ByteString.Char8 as B
import qualified Data.Vector as V
import qualified Data.Vector.Mutable as MV

main=do
  n<-readLn::IO Int
  as<-unfoldr (B.readInt.B.dropWhile(==' '))<$>B.getLine::IO[Int]
  bs<-unfoldr (B.readInt.B.dropWhile(==' '))<$>B.getLine::IO[Int]
  let buf = V.replicate n 0
  --
  print.sum.map (st buf n as bs) $ ts (2^28)
--
ts::Int->[Int]
ts t
  | t==1 = [1]
  | True = t:(ts $ t`div`2)

st::V.Vector Int->Int->[Int]->[Int]->Int->Int
st v' n as bs' t = if even$ sum $ map (f n v t) as then 0 else t
  where v = bmod v' (t*2) bs'

bmod::V.Vector Int -> Int -> [Int]->V.Vector Int
bmod v' t bs = runST $ do
  v <- V.unsafeThaw v'
  forM_ (zip [0..] bs) $ \(i,b) -> MV.unsafeWrite v i (b`mod`t)
  sortv (<) v 0 (V.length v' - 1)
  V.unsafeFreeze v
--
f::Int->V.Vector Int->Int->Int->Int
f n bs t a' = b2 - b1 + b4 - b3
  where
    a = a' `mod` (t*2)
    b1 = bsearch (\i-> (bs V.! i) < (t-a)) (-1,n)
    b2 = bsearch (\i-> (bs V.! i) < (2*t-a)) (-1,n)
    b3 = bsearch (\i-> (bs V.! i) < (3*t-a)) (-1,n)
    b4 = bsearch (\i-> (bs V.! i) < (4*t-a)) (-1,n)

bsearch f (a,b)
    | a+1 == b = a
    | f m  = bsearch f (m,b)
    | True = bsearch f (a,m)
    where m = (a + (b-a)`div`2)
--
{-# INLINE sortv #-}
sortv :: (a->a->Bool) -> MV.STVector s a -> Int -> Int -> ST s ()
sortv f as b e = do
        l <- MV.unsafeRead as b
        r <- MV.unsafeRead as e
        p <- findP l (b+1) e
        case p of
            Nothing -> return ()
            Just piv -> do
                k <- partv f as piv b e
                sortv f as b (k-1)
                sortv f as k e
    where
        findP a i e
            | i > e = return Nothing
            | True = do
                k <- MV.unsafeRead as i
                case (f a k, f k a) of
                    (True, _) -> return $ Just k
                    (_, True) -> return $ Just a
                    _ -> findP a (i+1) e

partv :: (a->a->Bool) -> MV.STVector s a -> a -> Int -> Int -> ST s Int
partv f as piv b e = do
    MV.unsafeSwap as b e
    l <- findF b
    r <- findT e
    if l <= r then partv f as piv l r
              else return l
    where
        findF i = do
            k <- MV.unsafeRead as i
            if f k piv then findF (i+1) else return i
        findT j = do
            k <- MV.unsafeRead as j
            if f k piv then return j else findT (j-1)
