import           Control.Exception (assert)
import           Data.List         (permutations, sort)

main :: IO ()
main = do
  n <- readLn
  p <- map read . words <$> getLine
  q <- map read . words <$> getLine
  assert (length p == n && length q == n) $
    print $ solve n p q

solve :: Int -> [Int] -> [Int] -> Int
solve n p q
  | p < q     = solve' n p q
  | otherwise = solve' n q p

solve' :: Int -> [Int] -> [Int] -> Int
solve' n p q = length $ takeWhile (q /=) $ dropWhile (p /=) $ allPermutation n

allPermutation :: Int -> [[Int]]
allPermutation n = sort $ permutations [1..n]
