import Data.List

main = do
  n <- readLn :: IO Int
  ps <- map read . words <$> getLine :: IO [Int]
  qs <- map read . words <$> getLine :: IO [Int]
  putStrLn $ show $ dist ps qs $ sort $ permutations [1..n]

foldlState :: (a -> c -> b -> (a, c)) -> a -> c -> [b] -> a
foldlState f a s l = fst $ foldl (\(a,s) -> f a s) (a, s) l

dist ps qs order | ps == qs = 0
                 | otherwise = foldlState step 0 False order
    where step a b rs = let c = (rs == ps || rs == qs) in (a + if b then 1 else 0, if b then not c else c)
