Scanf.scanf "%d" (fun n ->
    let module S = Set.Make (struct type t = int * int let compare = compare end) in
    let a = Array.init n (fun _ -> Scanf.scanf " %d" (fun a -> a)) in
    let b = Array.make n 0 in 
    let rec loop i set =
        if i = n then set else
            let set = Scanf.scanf " %d" (fun k -> b.(i) <- k; S.add (k, i) set) in
            loop (i + 1) set
    in
    let set = loop 0 S.empty in

    let check set = S.for_all (fun (k, i) -> a.(i) = k) set in

    let rec loop set c =
        let (k, i) = S.max_elt set in
        let set = S.remove (k, i) set in
        let (k2 ,i2) = S.max_elt set in
        let delta = b.((i - 1 + n) mod n) + b.((i + 1) mod n) in
        let q = (k - k2 + delta) / delta in
        let nb = b.(i) - q * delta in
        if nb < a.(i) then (if check set then c + q - 1 else -1) else (
            b.(i) <- nb;
            loop (S.add (nb, i) set) (c + q)
        )
    in
    loop set 0 |> Printf.printf "%d\n"
)