open System
open System.Collections.Generic

module InputUtility =
    let InputTokens = new Queue<string>()

    let EnsureInput () =
        if InputTokens.Count = 0 then
            let input = Console.ReadLine ()
            let s = input.Split ([|' '|], StringSplitOptions.RemoveEmptyEntries)
            if s.Length > 0 then
                for x in s do InputTokens.Enqueue(x)
                true
            else false
        else true

    let rec ReadNext () =
        if EnsureInput () then InputTokens.Dequeue ()
        else ReadNext ()

    let ReadNextInt () = ReadNext () |> int

    let Read2Int () = ReadNextInt (), ReadNextInt ()

    let Read3Int () = ReadNextInt (), ReadNextInt (), ReadNextInt ()

    let ReadListOf f n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop ((ReadNext () |> f) :: acc) (c-1)
        loop [] n

    let MultipleCaseWithId f =
        let caseNum = ReadNextInt ()
        let rec loop index =
            if index = caseNum then ()
            else
                f index
                loop (index + 1)
        loop 0

    let MultipleCase f = MultipleCaseWithId (fun _ -> f ())

    let ReadList n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop (ReadNext () :: acc) (c-1)
        loop [] n

open InputUtility

module AGC016 =
    (* node index ranges from 0 to n-1 *)
    type DisjointSetArray (n) =
        let rootValue = -1
        let arr = Array.create n rootValue

        (* Gets the root node of the set x belongs to. *)
        member s.GetRoot x =
            if arr.[x] = -1 then x
            else
                arr.[x] <- s.GetRoot arr.[x]
                arr.[x]

        (* Gets the root nodes. *)
        member s.GetRoots () = [0..n-1] |> Seq.filter (fun x -> arr.[x] = rootValue)

        (* Merges the sets x and y each belongs to.
         * Input of f: root node of x and root node of y. *)
        member s.Merge x y f =
            let rx, ry = s.GetRoot x, s.GetRoot y
            if rx <> ry then arr.[rx] <- ry
            f rx ry

    let private ``XOR Replace`` () =
        let n = ReadNextInt ()
        let ga () =
            let a = ReadListOf int n
            let r = a @ [a |> Seq.reduce (^^^)]
            r |> Seq.toArray, r |> Seq.sort |> Seq.cache
        let (a, sa), (b, sb) = ga (), ga ()
        if Seq.forall2 (=) sa sb |> not then -1
        else
            let ids =
                sa
                |> Seq.mapi (fun i x -> x, i)
                |> Seq.groupBy fst
                |> Seq.map (fun (x, s) -> x, s |> Seq.minBy snd)
                |> Map.ofSeq
            let ds = new DisjointSetArray (ids.Count)
            let cnt = Array.create ids.Count 1
            let es = [0..n-1] |> Seq.where (fun i -> a.[i] <> b.[i]) |> Seq.cache
            es |> Seq.iter (fun i ->
                ds.Merge a.[i] b.[i] (fun x y ->
                    if x <> y then cnt.[y] <- cnt.[x] + cnt.[y]))
            let rn = ds.GetRoots () |> Seq.where (fun x -> cnt.[x] > 1) |> Seq.length
            let inLoop = ds.GetRoot a.[n] <> a.[n] || cnt.[a.[n]] > 1
            printfn "%A %d %A\n%A" es rn inLoop (ds.GetRoots () |> Seq.map (fun i -> i, cnt.[i]))
            Seq.length es + rn + (if inLoop then -1 else 0)
        |> printfn "%d"

    let Entry = ``XOR Replace``

AGC016.Entry ()