import Control.Monad
import Data.List
import Data.Maybe

main = do
    n <- readLn :: IO Int
    pn <- map read . words <$> getLine :: IO [Int]
    qn <- map read . words <$> getLine :: IO [Int]
    let n' = sort $ permutations [1 .. n]
    let pn' = fromJust $ elemIndex pn n'
    let qn' = fromJust $ elemIndex qn n'
    print $ abs $ pn' - qn'