open System


module List =
    let rec inserts x = function
        | [] -> [[x]]
        | z::zs as ys-> (x::ys) :: [for ys in inserts x zs -> z::ys]
    let rec perms = function
        | [] -> [[]]
        | x::xs -> List.collect (inserts x) (perms xs)

let n = stdin.ReadLine() |> int
let p = stdin.ReadLine().Split() |> Array.map int |> List.ofArray
let q = stdin.ReadLine().Split() |> Array.map int |> List.ofArray
            
let perms = List.perms [1 .. n] |> List.sort

let a = Seq.findIndex ((=) p) perms
let b = Seq.findIndex ((=) q) perms

a - b
|> abs
|> printfn "%d"
