import sys

input = sys.stdin.readline

sys.setrecursionlimit(10 ** 7)



from heapq import heappop, heappush



N = int(eval(input()))

A = [int(x) for x in input().split()]

B = [int(x) for x in input().split()]



q = []

push = lambda x,i: heappush(q, (-x<<30)+i)

for i,x in enumerate(B):

    push(x,i)



answer = 0

mask = (1<<30)-1

while q:

    i = heappop(q)&mask

    x = B[i]

    dx = x-A[i]

    if dx < 0:

        answer = -1

        break

    if dx == 0:

        continue

    left = i-1 if i >= 1 else N-1

    right = i+1 if i < N-1 else 0

    nbd = B[left] + B[right]

    t = dx // nbd

    if t == 0:

        answer = -1

        break

    B[i] -= nbd * t

    answer += t

    push(B[i],i)



print(answer)
import sys

import numpy as np

import numba

from numba import njit, b1, i4, i8, f8



read = sys.stdin.buffer.read

readline = sys.stdin.buffer.readline

readlines = sys.stdin.buffer.readlines



@njit((i8[:], i8[:]), cache=True)

def main(A, B):

    N = len(A)

    st, p = np.arange(1 << 20), N

    ans = 0

    while p:

        i, p = st[p - 1], p - 1

        i %= N

        a, b, c = B[i - 1], B[i], B[(i + 1) % N]

        if b < a + c:

            continue

        if b == A[i]:

            continue

        k = (B[i] - A[i]) // (a + c)

        ans += k

        B[i] -= (a + c) * k

        st[p], p = i - 1, p + 1

        st[p], p = i + 1, p + 1

    return ans if np.all(A == B) else -1



N = int(readline())

A = np.array(readline().split(), np.int64)

B = np.array(readline().split(), np.int64)



print((main(A, B)))
import sys

input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from heapq import heappop, heappush

N = int(eval(input()))
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
q = []
push = lambda x, i: heappush(q, (-x << 30) + i)
for i, x in enumerate(B):
    push(x, i)
answer = 0
mask = (1 << 30) - 1
while q:
    i = heappop(q) & mask
    x = B[i]
    dx = x - A[i]
    if dx < 0:
        answer = -1
        break
    if dx == 0:
        continue
    left = i - 1 if i >= 1 else N - 1
    right = i + 1 if i < N - 1 else 0
    nbd = B[left] + B[right]
    t = dx // nbd
    if t == 0:
        answer = -1
        break
    B[i] -= nbd * t
    answer += t
    push(B[i], i)
print(answer)

import sys
import numpy as np
import numba
from numba import njit, b1, i4, i8, f8

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


@njit((i8[:], i8[:]), cache=True)
def main(A, B):
    N = len(A)
    st, p = np.arange(1 << 20), N
    ans = 0
    while p:
        i, p = st[p - 1], p - 1
        i %= N
        a, b, c = B[i - 1], B[i], B[(i + 1) % N]
        if b < a + c:
            continue
        if b == A[i]:
            continue
        k = (B[i] - A[i]) // (a + c)
        ans += k
        B[i] -= (a + c) * k
        st[p], p = i - 1, p + 1
        st[p], p = i + 1, p + 1
    return ans if np.all(A == B) else -1


N = int(readline())
A = np.array(readline().split(), np.int64)
B = np.array(readline().split(), np.int64)
print((main(A, B)))

+import numpy as np +import numba +from numba import njit, b1, i4, i8, f8 -input = sys.stdin.readline -sys.setrecursionlimit(10**7) -from heapq import heappop, heappush +read = sys.stdin.buffer.read +readline = sys.stdin.buffer.readline +readlines = sys.stdin.buffer.readlines -N = int(eval(input())) -A = [int(x) for x in input().split()] -B = [int(x) for x in input().split()] -q = [] -push = lambda x, i: heappush(q, (-x << 30) + i) -for i, x in enumerate(B): -    push(x, i) -answer = 0 -mask = (1 << 30) - 1 -while q: -    i = heappop(q) & mask -    x = B[i] -    dx = x - A[i] -    if dx < 0: -        answer = -1 -        break -    if dx == 0: -        continue -    left = i - 1 if i >= 1 else N - 1 -    right = i + 1 if i < N - 1 else 0 -    nbd = B[left] + B[right] -    t = dx // nbd -    if t == 0: -        answer = -1 -        break -    B[i] -= nbd * t -    answer += t -    push(B[i], i) -print(answer) + +@njit((i8[:], i8[:]), cache=True) +def main(A, B): +    N = len(A) +    st, p = np.arange(1 << 20), N +    ans = 0 +    while p: +        i, p = st[p - 1], p - 1 +        i %= N +        a, b, c = B[i - 1], B[i], B[(i + 1) % N] +        if b < a + c: +            continue +        if b == A[i]: +            continue +        k = (B[i] - A[i]) // (a + c) +        ans += k +        B[i] -= (a + c) * k +        st[p], p = i - 1, p + 1 +        st[p], p = i + 1, p + 1 +    return ans if np.all(A == B) else -1 + + +N = int(readline()) +A = np.array(readline().split(), np.int64) +B = np.array(readline().split(), np.int64) +print((main(A, B)))