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 =
    type 'a Q = System.Collections.Generic.Queue<'a>

    let private ``XOR Replace`` () =
        let n = ReadNextInt ()
        let ga () =
            let a = ReadListOf int n |> Seq.toArray
            a, a |> Seq.reduce (^^^)
        let a, xa = ga ()
        let b, xb = ga ()
        let diff =
            let dic = new System.Collections.Generic.Dictionary<_, _> ()
            Seq.mapi2 (fun i x y -> if x <> y then Some (y, i) else None) a b
            |> Seq.where Option.isSome
            |> Seq.map Option.get
            |> Seq.groupBy fst
            |> Seq.iter (fun (x, s) -> dic.[x] <- new Q<_> (s |> Seq.map snd))
            dic
        let getNext x =
            match diff.TryGetValue x with
            | false, _ -> None
            | true, q ->
                if q.Count = 0 then
                    diff.Remove x |> ignore
                    None
                else q.Dequeue () |> Some
        let add x i = diff.[x].Enqueue i
        let dq = new Q<_> (diff.Keys)
        let rec loop sx tx x c =
            let rec startNew r =
                if dq.Count = 0 then r
                else
                    let y = dq.Dequeue ()
                    //printfn "\tstart %d" y
                    match getNext y with
                    | None -> startNew r
                    | Some i ->
                        let z = a.[i]
                        a.[i] <- x
                        if x <> y then add y i
                        //printfn "\tpick %d(%d)" i z
                        loop sx tx z (c + 1)
            //printfn "[%d] %d : %A" c x a
            match getNext x with
            | None ->
                if x = sx then startNew c
                elif x = tx then c
                else -1
            | Some i ->
                let z = a.[i]
                a.[i] <- x
                //printfn "\tpick %d(%d)" i z
                loop sx tx z (c + 1)
        loop xa xb xa 0 |> printfn "%d"

    let Entry = ``XOR Replace``

AGC016.Entry ()