import Data.List
import Data.Maybe

main = do
    n <- readLn
    p <- map read . words <$> getLine
    q <- map read . words <$> getLine
    let sorted_comb = sort $ permutations [1 .. n]
    let a = elemIndex p sorted_comb
    let b = elemIndex q sorted_comb
    print $ abs $ fromJust b - fromJust a