import           Data.List
main = do
        n <- readLn :: IO Int
        p <- map read . words <$> getLine :: IO [Int]
        q <- map read . words <$> getLine :: IO [Int]
        print $ do
                i <- elemIndex q $ sort $ permutations p
                return $ i - 1
