{-# OPTIONS_GHC -O #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ViewPatterns #-}

import Control.Monad
import Data.List
import Data.Bits ((.&.), xor)
import qualified Data.ByteString.Char8 as B

shaku :: Int -> [Int] -> [Int] -> Int -> Int -> Int
shaku n _ [] _ _ = n
shaku n la@(l:ls) ra@(r:rs) c t =
    case t .&. r of
        0  -> shaku (n+c) la rs (c+1) $ xor t r
        t' -> shaku n ls ra (c-1) $ xor t l

main = do
    _ <- getLine
    as <- unfoldr (B.readInt.B.dropWhile (==' ')) <$> B.getLine
    print $ shaku 0 as as 1 0
