let combine =
  fun n ->
    let rec _combine = fun list ->
      match list with
      | [] -> []
      | [a] -> [[a]]
      | [a; b] -> [[a; b]; [b; a]]
      | _ ->
        list                                    // [1; 2; 3]
        |> List.collect (fun x ->               // 1
            list
            |> List.filter (fun x2 -> x <> x2)  // [2; 3]
            |> _combine                         // [[2; 3]; [3; 2]]
            |> List.map  (fun ts -> x::ts)      // [[1; 2; 3]; [1; 3; 2]]
        )
    [1..n]
    |> _combine
    |> List.mapi (
      fun i order ->
        (
          i,
          order
          |> List.map string
          |> List.reduce (fun a b -> a + " " + b)
        )
    )

(
  stdin.ReadLine() |> int,
  stdin.ReadLine(),
  stdin.ReadLine()
)
|> fun (n, ps, qs) -> (n |> combine, ps, qs)
|> fun (dict, ps, qs) ->
  (
    (dict |> List.find (fun (_, v) -> v = ps) |> fun (i, _) -> i),
    (dict |> List.find (fun (_, v) -> v = qs) |> fun (i, _) -> i)
  )
|> fun (a, b) -> (a - b) |> abs
|> printfn "%d"
