import qualified Data.ByteString.Char8 as BS
import Data.Maybe
import Data.List

readInt = fromIntegral . fst . fromJust . BS.readInteger
readIntList = map readInt . BS.words

getInt = readInt <$> BS.getLine
getIntList = readIntList <$> BS.getLine

sortPermutations :: [[Int]] -> [[Int]]
sortPermutations xs = sortBy (\x y -> compare (x !! 0) (y !! 0)) (sortBy (\x y -> compare (x !! 1) (y !! 1)) (sortBy (\x y -> compare (x !! 2) (y !! 2)) xs))

main = do
  n <- getInt
  ps <- getIntList
  qs <- getIntList
  let ns = permutations [1..n]
  let ns' = sortPermutations ns
  let a = fromJust(elemIndex ps ns')
  let b = fromJust(elemIndex qs ns')
  print $ abs (a - b)