main = do
    n <- getInt
    ps <- getIntList
    qs <- getIntList
    let as = sort $ permutations [1..n]
    let p = fromJust $ elemIndex ps as
    let q = fromJust $ elemIndex qs as
    print . abs $ p - q