import Data.List
import Data.Maybe

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