let () =
    let fact = [| 1; 1; 2; 6; 24; 120; 720; 5040 |] in
    let read_seq n =
        let order mask c =
            let rec loop m c a =
                if c = 0 then a else
                    loop (m lsr 1) (c - 1) (if m land 1 = 1 then a else a + 1)
            in
            loop mask c 0
        in
        let rec loop mask acc n =
            if n = 0 then acc else
                let c = Scanf.scanf " %d" (fun c -> c - 1) in
                let o = order mask c in
                loop (mask lor (1 lsl c)) (acc + fact.(n - 1) * o) (n - 1)
        in
        loop 0 0 n
    in
    let main () =
        let n = read_line () |> int_of_string in
        let a = read_seq n in
        let b = read_seq n in
        Printf.printf "%d\n" (abs (a - b))
    in
    main ()