{-# LANGUAGE FlexibleInstances #-}

import Data.List

-- stdin
class In a where stdin :: IO a
instance In [Int] where stdin = map read . words <$> getLine

getIndex :: [Int] -> Int
getIndex ns = indexOf ns $ sort $ permutations [1..(length ns)]
  where
    indexOf x xs = length $ takeWhile (/=x) $ sort xs

main = do
    _ <- getLine
    a <- stdin
    b <- stdin

    print $ abs (getIndex a - getIndex b)
