CodeEff/ECCO huggingface dataset:
# Original solution
from heapq import heappush,heappop
n,*t=list(map(int,open(0).read().split()))
a=t[:n]
b=t[n:]
B=[]
for i,j in enumerate(b):heappush(B,(-j,i))
c=0
d=0
while B and d<2*10**6:
    d+=1
    y,i=heappop(B)
    x,z=b[(i-1)%n],b[(i+1)%n]
    t=(b[i]-a[i])//(x+z)
    b[i]-=t*(x+z)
    c+=t
    if b[i]>a[i]:
        heappush(B,(-b[i],i))
print(([-1,c][a==b]))

# Optimized solution
n,*t=list(map(int,open(0).read().split()));A=t[:n];B=t[n:];r=0
while True:
    c=0
    for i in range(n):
        b=B[(i-1)%n]+B[(i+1)%n]
        if 0<b<B[i] and A[i]<B[i]:
            t=(B[i]-A[i])//b
            c+=t
            B[i]-=t*b
    r+=c
    if c==0:break
    
print(([-1,r][A==B]))
# Original solution
n,*t=list(map(int,open(0).read().split()));A=t[:n];B=t[n:];r=0
while 1:
 c=0
 for i in range(n):
  b=B[~-i%n]+B[-~i%n]
  if b<B[i]and A[i]<B[i]:t=(B[i]-A[i])//b;c+=t;B[i]-=t*b
 r+=c
 if c==0:break
print(([-1,r][A==B]))

# Optimized solution
n,*t=list(map(int,open(0).read().split()));A=t[:n];B=t[n:];r=0
while 1:
 c=0
 for i in range(n):
  b=B[~-i%n]+B[-~i%n]
  if A[i]<B[i]>b:t=(B[i]-A[i])//b;c+=t;B[i]-=t*b
 r+=c
 if c==0:break
print(([-1,r][A==B]))
# Original solution
def examA():
    S = SI(); N = len(S)
    prev = S[0]; cur = ""
    k = 0; ans = 1
    while(k<N-1):
        k +=1
        cur += S[k]
        if cur==prev:
            continue
        prev = cur
        cur = ""
        ans +=1
    print(ans)
    return

def examB():
    N = I()
    S = SI()
    return

def examC():
    N = I()
    A = LI()
    B = LI()
    ans = 0
    que = []
    for i in range(N):
        que.append((-B[i],i))
    heapify(que)
    flag = True
    while(que):
        b,i = heappop(que)
        b *=(-1)
        if b == A[i]:
            continue
        a, c = B[(i - 1) % N], B[(i + 1) % N]
        if b - A[i] < a + c:
            flag = False
            break
        r = (b - A[i]) // (a + c)
        b -= (a + c) * r
        B[i] = b
        ans += r
        if b != A[i]:
            heapq.heappush(que, (-b, i))
#    print(que)
#    print(K)
    if flag:
        print(ans)
    else:
        print((-1))
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18

if __name__ == '__main__':
    examC()


# Optimized solution
def examA():
    S = SI(); N = len(S)
    prev = S[0]; cur = ""
    k = 0; ans = 1
    while(k<N-1):
        k +=1
        cur += S[k]
        if cur==prev:
            continue
        prev = cur
        cur = ""
        ans +=1
    print(ans)
    return

def examB():
    N = I()
    S = SI()
    return

def examC():
    N = I()
    A = LI()
    B = LI()
    ans = 0
    que = []
    for i in range(N):
        heappush(que,(-B[i],i))
    flag = True
    while(que):
        b,i = heappop(que)
        b *=(-1)
        if b == A[i]:
            continue
        a, c = B[(i - 1) % N], B[(i + 1) % N]
        if b - A[i] < a + c:
            flag = False
            break
        r = (b - A[i]) // (a + c)
        b -= (a + c) * r
        B[i] = b
        ans += r
        if b != A[i]:
            heapq.heappush(que, (-b, i))
#    print(que)
#    print(K)
    if flag:
        print(ans)
    else:
        print((-1))
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18

if __name__ == '__main__':
    examC()
# Original solution
import sys
input = sys.stdin.readline

N=int(eval(input()))
A=list(map(int,input().split()))
B=list(map(int,input().split()))
BI=[(b,i) for i,b in enumerate(B)]

# Segment tree(1-indexed,再帰を使わないもの)

seg_el=1<<(N.bit_length())# Segment treeの台の要素数
SEG=[(0,0)]*(2*seg_el)# 1-indexedなので、要素数2*seg_el.Segment treeの初期値で初期化

for i in range(N):
    SEG[i+seg_el]=BI[i]

for i in range(seg_el-1,0,-1):# 親の部分もupdate
    SEG[i]=max(SEG[i*2],SEG[i*2+1])

def update(n,x,seg_el):# A[n]をxへ更新（反映）
    i=n+seg_el
    SEG[i]=x
    i>>=1# 子ノードへ
    
    while i!=0:
        SEG[i]=max(SEG[i*2],SEG[i*2+1])
        i>>=1
        
def getvalues(l,r):# 区間[l,r)に関するmaxを調べる
    L=l+seg_el
    R=r+seg_el
    ANS=(0,0)

    while L<R:
        if L & 1:
            ANS=max(ANS , SEG[L])
            L+=1

        if R & 1:
            R-=1
            ANS=max(ANS , SEG[R])
        L>>=1
        R>>=1

    return ANS

ANS=0
while True:
    x,y=getvalues(0,N)
    if x==0:
        break
    if x<A[y]:
        print((-1))
        sys.exit()

    elif x==A[y]:
        update(y,(0,y),seg_el)

    else:
        B[y]-=B[(y+1)%N]+B[(y-1)%N]
        ANS+=1
        update(y,(B[y],y),seg_el)

print(ANS)
        

            


# Optimized solution
import sys
input = sys.stdin.readline

N=int(eval(input()))
A=list(map(int,input().split()))
B=list(map(int,input().split()))
BI=[(b,i) for i,b in enumerate(B)]

# Segment tree(1-indexed,再帰を使わないもの)

seg_el=1<<(N.bit_length())# Segment treeの台の要素数
SEG=[(0,0)]*(2*seg_el)# 1-indexedなので、要素数2*seg_el.Segment treeの初期値で初期化

for i in range(N):
    SEG[i+seg_el]=BI[i]

for i in range(seg_el-1,0,-1):# 親の部分もupdate
    SEG[i]=max(SEG[i*2],SEG[i*2+1])

def update(n,x,seg_el):# A[n]をxへ更新（反映）
    i=n+seg_el
    SEG[i]=x
    i>>=1# 子ノードへ
    
    while i!=0:
        SEG[i]=max(SEG[i*2],SEG[i*2+1])
        i>>=1
        
def getvalues(l,r):# 区間[l,r)に関するmaxを調べる
    L=l+seg_el
    R=r+seg_el
    ANS=(0,0)

    while L<R:
        if L & 1:
            ANS=max(ANS , SEG[L])
            L+=1

        if R & 1:
            R-=1
            ANS=max(ANS , SEG[R])
        L>>=1
        R>>=1

    return ANS

ANS=0
while True:
    x,y=getvalues(0,N)
    if x==0:
        break
    if x<A[y]:
        print((-1))
        sys.exit()

    elif x==A[y]:
        update(y,(0,y),seg_el)

    else:
        t=B[(y+1)%N]+B[(y-1)%N]
        if B[y]<t+A[y]:
            print((-1))
            sys.exit()
            
        q=(B[y]-A[y])//t
        ANS+=q
        B[y]-=t*q
        update(y,(B[y],y),seg_el)

print(ANS)
# Original solution
from heapq import heappop,heappush
def main(a,b):
    for i in range(n):
        if b[i]<=a[i]:
            pass
        else:
            print((-1))
            exit()
    pq=[]
    [heappush(pq,[-x,i]) for i,x in enumerate(a)]
    ans=0
    while pq:
        v,i=heappop(pq)
        v=-v
        vv=v
        v-=b[i]
        tmp=a[(i-1)%n]+a[(i+1)%n]
        ans+=v//tmp
        v%=tmp
        v+=b[i]
        a[i]=v
        if v==b[i]:continue
        elif vv<=v or v<b[i]:
            print((-1))
            exit()
        heappush(pq,[-v,i])
    print(ans)
if __name__=='__main__':
    n=int(eval(input()))
    b=list(map(int,input().split()))
    a=list(map(int,input().split()))
    main(a,b)


# Optimized solution
def main(n,a,b):
    ans=0
    while True:
        cnt=0
        for i in range(n):
            m=a[(i-1)%n]+a[(i+1)%n]
            if m<a[i] and b[i]<a[i]:
                a[i]-=b[i]
                cnt+=a[i]//m
                a[i]%=m
                a[i]+=b[i]
        if cnt==0:break
        ans+=cnt
    if a==b:
        print(ans)
    else:
        print((-1))
if __name__=='__main__':
    n=int(eval(input()))
    b=list(map(int,input().split()))
    a=list(map(int,input().split()))
    main(n,a,b)
# Original solution
def p_c():
    N = int(eval(input()))
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    l = [-1] * N
    idx = set(range(N))

    for i in range(N):
        l[i] = B[i - 1] + B[(i + 1) % N]

    ans = 0

    while 1:
        f = False
        for i in list(idx):
            if A[i] <= B[i] - l[i]:
                f = True
                ans += 1
                B[i] -= l[i]
                l[i - 1] -= l[i]
                l[(i + 1) % N] -= l[i]
                idx.add((N + i - 1) % N)
                idx.add((i + 1) % N)
            else:
                idx.remove(i)
        if not f:
            break

    if A != B:
        print((-1))
    else:
        print(ans)


if __name__ == '__main__':
    p_c()


# Optimized solution
def p_c():
    N = int(eval(input()))
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    l = [-1] * N

    for i in range(N):
        l[i] = B[i - 1] + B[(i + 1) % N]

    ans = 0

    while 1:
        f = False
        for i in range(N):
            if A[i] <= B[i] - l[i]:
                f = True
                n = (B[i] - A[i]) // l[i]
                ans += n
                B[i] -= n * l[i]
                l[i - 1] -= n * l[i]
                l[(i + 1) % N] -= n * l[i]
        if not f:
            break

    if A != B:
        print((-1))
    else:
        print(ans)


if __name__ == '__main__':
    p_c()
# Original solution
import heapq
N = int(eval(input()))
A = list(map(int,input().split()))
B = list(map(int,input().split()))

if any(a>b for a,b in zip(A,B)):
    print((-1))
    exit()

hq = [(-a,i) for i,a in enumerate(B)]
heapq.heapify(hq)

ans = 0
while hq:
    _,i = heapq.heappop(hq)
    if B[i] == A[i]: continue
    l = B[(i-1)%N]
    r = B[(i+1)%N]
    if B[i]-l-r < A[i]: continue
    k = (B[i]-A[i]) // (l+r)
    ans += k
    B[i] -= k*(l+r)
    heapq.heappush(hq, (-B[i],i))

if all(a==b for a,b in zip(A,B)):
    print(ans)
else:
    print((-1))

# Optimized solution
import heapq
N = int(eval(input()))
A = list(map(int,input().split()))
B = list(map(int,input().split()))

if any(a>b for a,b in zip(A,B)):
    print((-1))
    exit()

hq = []
for i,(a,b) in enumerate(zip(A,B)):
    if a <= b - B[(i-1)%N] - B[(i+1)%N]:
        hq.append((-b,i))
heapq.heapify(hq)

ans = 0
while hq:
    _,i = heapq.heappop(hq)
    if B[i] == A[i]: continue
    l = B[(i-1)%N]
    r = B[(i+1)%N]
    if B[i]-l-r < A[i]: continue
    k = (B[i]-A[i]) // (l+r)
    ans += k
    B[i] -= k*(l+r)
    if A[i] <= B[i] - l - r:
        heapq.heappush(hq, (-B[i],i))
    ll = B[(i-2)%N]
    rr = B[(i+2)%N]
    if A[(i+1)%N] <= r - B[i] - rr:
        heapq.heappush(hq, (-r,(i+1)%N))
    if A[(i-1)%N] <= l - B[i] - ll:
        heapq.heappush(hq, (-l,(i-1)%N))

if all(a==b for a,b in zip(A,B)):
    print(ans)
else:
    print((-1))
# Original solution
import heapq
N = int(eval(input()))
A = list(map(int,input().split()))
B = list(map(int,input().split()))

if any(a>b for a,b in zip(A,B)):
    print((-1))
    exit()

def pb(i):
    return i-1 if i-1 >= 0 else N-1
def nx(i):
    return i+1 if i+1 < N else 0

hq = []
for i,(a,b) in enumerate(zip(A,B)):
    if a <= b - B[pb(i)] - B[nx(i)]:
        hq.append((-b,i))
heapq.heapify(hq)

ans = 0
while hq:
    _,i = heapq.heappop(hq)
    if B[i] == A[i]: continue
    l = B[pb(i)]
    r = B[nx(i)]
    if B[i]-l-r < A[i]: continue
    k = (B[i]-A[i]) // (l+r)
    ans += k
    B[i] -= k*(l+r)
    if A[i] <= B[i] - l - r:
        heapq.heappush(hq, (-B[i],i))
    ll = B[pb(pb(i))]
    rr = B[nx(nx(i))]
    if A[nx(i)] <= r - B[i] - rr:
        heapq.heappush(hq, (-r,nx(i)))
    if A[pb(i)] <= l - B[i] - ll:
        heapq.heappush(hq, (-l,pb(i)))

if all(a==b for a,b in zip(A,B)):
    print(ans)
else:
    print((-1))

# Optimized solution
import heapq
N = int(eval(input()))
A = list(map(int,input().split()))
B = list(map(int,input().split()))

if any(a>b for a,b in zip(A,B)):
    print((-1))
    exit()

def pb(i):
    return i-1 if i-1 >= 0 else N-1
def nx(i):
    return i+1 if i+1 < N else 0

hq = []
for i,(a,b) in enumerate(zip(A,B)):
    if a <= b - B[pb(i)] - B[nx(i)]:
        hq.append((-b,i))
heapq.heapify(hq)

ans = 0
while hq:
    _,i = heapq.heappop(hq)
    if B[i] == A[i]: continue
    l = B[pb(i)]
    r = B[nx(i)]
    if B[i]-l-r < A[i]: continue
    k = (B[i]-A[i]) // (l+r)
    ans += k
    B[i] -= k*(l+r)
    ll = B[pb(pb(i))]
    rr = B[nx(nx(i))]
    if A[nx(i)] <= r - B[i] - rr:
        heapq.heappush(hq, (-r,nx(i)))
    if A[pb(i)] <= l - B[i] - ll:
        heapq.heappush(hq, (-l,pb(i)))

if all(a==b for a,b in zip(A,B)):
    print(ans)
else:
    print((-1))
# Original solution
from collections import deque

def main():
    N=int(eval(input()))
    A=list(map(int, input().split()))
    B=list(map(int, input().split()))
    q = deque()
    cq = deque()
    q.appendleft(B)
    cq.appendleft(0)

    while len(q) > 0:
        b = q.pop()
        c = cq.pop()
        if b == A:
            print(c)
            break
        else:
            for i in range(N):
                _tmp = b[0:N]
                _tmp[i] = b[i] - b[i-1 if i>=1 else N-1] - b[i+1 if i<N-1 else 0]
                if  A[i] <= _tmp[i]:
                    #print(_tmp)
                    q.appendleft(_tmp)
                    cq.appendleft(c+1)
    else:
        print((-1))

if __name__ == '__main__':
    main()

# Optimized solution
def main():
    N=int(eval(input()))
    A=list(map(int, input().split()))
    B=list(map(int, input().split()))

    count = 0
    while A!=B:
        b = True
        for i in range(N):
            d = B[i-1 if i>=1 else N-1] + B[i+1 if i<N-1 else 0]
            if B[i] > d > 0:
                b = False
                c = (B[i]-A[i]) // d
                B[i] -= d * c
                count += c
        if b:
            if A == B:
                print(count)
            else:
                print((-1))
            break
    else:
        if A == B:
            print(count)
        else:
            print((-1))

if __name__ == '__main__':
    main()
# Original solution
import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__

def unable():
    print((-1))
    exit()

n = int(eval(input()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))

from heapq import heappop, heappush
done = set()
heap = []
for i in range(n):
    if a[i] > b[i]:
        unable()
    elif a[i] == b[i]:
        done.add(i)
    elif b[i] >= b[i-1]+b[(i+1)%n]:
        heappush(heap, (-b[i],i))

ans = 0
while heap and len(done) != n:
    _, cur = heappop(heap)
    prv = (cur-1)%n
    nxt = (cur+1)%n
    base = b[prv]+b[nxt]
    cnt1, mod = divmod(b[cur], base)
    if cnt1 == 0:
        if b[cur] == a[cur]:
            done.add(cur)
        continue
    if mod > a[cur]:
        ans += cnt1
        b[cur] = mod
    else:
        cnt2, mod = divmod(b[cur]-a[cur], base)
        if mod != 0:
            unable()
        ans += cnt2
        done.add(cur)
        b[cur] = a[cur]
    if prv not in done:
        heappush(heap, (-b[prv], prv))
    if nxt not in done:
        heappush(heap, (-b[nxt], nxt))

if len(done)==n:
    print(ans)
else:
    unable()


# Optimized solution
import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__

def unable():
    print((-1))
    exit()

n = int(eval(input()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))

from heapq import heappush, heappop
h = []
for i in range(n):
    if a[i]>b[i]:
        unable()
    else:
        heappush(h, (-b[i], i))

ans = 0
while h:
    _, i = heappop(h)
    cnt, mod = divmod(b[i]-a[i],b[i-1]+b[(i+1)%n])
    if cnt == 0:
        if mod != 0:
            unable()
    else:
        ans += cnt
        b[i] = a[i]+mod
        heappush(h, (-b[i], i))
print(ans)
# Original solution
import sys,heapq as hq
mod = 10**9+7
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return list(map(int,input().split()))
def onem(): #Nとかの取得
    return int(eval(input()))


n = onem()

a = l()

b = l()

t = []

for i in range(n):
    if b[i] != a[i]:
        hq.heappush(t,(-b[i],i))
co = 0
while t:
    k = hq.heappop(t)
    k = (-k[0],k[1])
    q,w = divmod(k[0] - a[k[1]], b[(k[1] - 1) % n] + b[(k[1] + 1) % n])
    co += q
    if q == 0:
        print((-1))
        exit()
    w = a[k[1]] + w
    b[k[1]] = w
    if b[k[1]] != a[k[1]]:
        hq.heappush(t,(-w,k[1]))

print(co)

# Optimized solution
import heapq as hq
mod = 10**9+7
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return list(map(int,input().split()))
def onem(): #Nとかの取得
    return int(eval(input()))

def onon():
    n = onem()

    a = l()

    b = l()

    t = []

    for i in range(n):
        if b[i] != a[i]:
            hq.heappush(t,(-b[i],i))
    co = 0
    while t:
        k = hq.heappop(t)
        k = (-k[0],k[1])
        q,w = divmod(k[0] - a[k[1]], b[(k[1] - 1) % n] + b[(k[1] + 1) % n])
        co += q
        if q == 0:
            return -1
        w = a[k[1]] + w
        b[k[1]] = w
        if b[k[1]] != a[k[1]]:
            hq.heappush(t,(-w,k[1]))
    return co
print((onon()))
# Original solution
from heapq import heappush,heappop
import sys
input=sys.stdin.readline

N=int(eval(input()))
A=list(map(int,input().split()))
B=list(map(int,input().split()))
lst=[]
for i in range(N):
    heappush(lst,[-B[i],i])

count=0
while lst:
    h,i=heappop(lst)
    h=-h
    x,z=B[i-1],B[(i+1)%N]
    if (h-A[i])%(x+z)==0:
        num=(h-A[i])//(x+z)
        count+=num
        B[i]=A[i]
        continue
    else:
        num=h//(x+z)
        h=h%(x+z)
        count+=num
        if num==0:
            print((-1))
            sys.exit()
        B[i]=h    
        if A[i]>h:
            print((-1))
            sys.exit()
        heappush(lst,[-h,i])

if A==B:
    print(count)
else:
    print((-1))

# Optimized solution
from heapq import heappush,heappop
import sys
input=sys.stdin.readline

N=int(eval(input()))
A=list(map(int,input().split()))
B=list(map(int,input().split()))
lst=[]
for i in range(N):
    heappush(lst,(-B[i],i))

count=0
while lst:
    h,i=heappop(lst)
    h=-h
    x,z=B[i-1],B[(i+1)%N]
    kkk=x+z
    if (h-A[i])%kkk==0:
        count+=(h-A[i])//kkk
        B[i]=A[i]
        continue
    elif kkk>=h:
        print((-1))
        sys.exit()
    else:
        count+=h//kkk
        h=h%kkk
        B[i]=h    
        if A[i]>h:
            print((-1))
            sys.exit()
        heappush(lst,(-h,i))

if A==B:
    print(count)
else:
    print((-1))
# Original solution
import sys
input = sys.stdin.readline
sys.setrecursionlimit(100000)

def getN():
    return int(eval(input()))
def getList():
    return list(map(int, input().split()))
import math

n = getN()

anums = getList()
bnums = getList()
ans = 0

update = True
while(update):
    update = False
    for i in range(n):
        if i == n - 1:
            j = 0
        else:
            j = i + 1
        h = i - 1
        a,b,c = bnums[h], bnums[i], bnums[j]
        # print(a,b,c)
        if b > max(a, c):
            if anums[i] > bnums[i]:
                print((-1))
                sys.exit()
            if anums[i] != bnums[i]:
                dec = math.ceil((b - (max(a, c, anums[i])) ) / (a+c))
                bnums[i] -= dec * (a+c)
                ans += dec
                update = True
    # print(coll, anums, bnums)

print(ans)

# Optimized solution
import sys
input = sys.stdin.readline
sys.setrecursionlimit(100000)

def getN():
    return int(eval(input()))
def getList():
    return list(map(int, input().split()))
import math

n = getN()

anums = getList()
bnums = getList()
ans = 0
okay = [0 for i in range(n)]
update = True
while(update):
    update = False
    for i in range(n):
        if not okay[i]:
            if i == n - 1:
                j = 0
            else:
                j = i + 1
            h = i - 1
            a,b,c, tgt = bnums[h], bnums[i], bnums[j], anums[i]
            # print(a,b,c)
            if b > max(a, c):
                if tgt > b:
                    print((-1))
                    sys.exit()
                if tgt != b:
                    dec = math.ceil((b - (max(a, c, tgt)) ) / (a+c))
                    bnums[i] -= dec * (a+c)
                    ans += dec
                    update = True
                    if anums[i] == bnums[i]:
                        okay[i] = 1
    # print(coll, anums, bnums)

print(ans)
# Original solution
from heapq import *

N = int(eval(input()))
A = [int(a) for a in input().split()]
B = [int(a) for a in input().split()]

h = []
for i in range(N):
    heappush(h, (-B[i], i))

ans = 0
while h:
    b, i = heappop(h)
    b = -b
    if b == A[i]:
        continue
    k = - (-(B[i] - max(B[i-1], B[(i+1)%N], A[i])) // (B[i-1] + B[(i+1)%N]))
    if k <= 0:
        print("-1")
        break
    B[i] -= (B[i-1] + B[(i+1)%N]) * k
    heappush(h, (-B[i], i))
    ans += k
else:
    print(ans)


# Optimized solution
N = int(eval(input()))
A = [int(a) for a in input().split()]
B = [int(a) for a in input().split()]
if N > 10000:
    A = A[5050:] + A[:5050]
    B = B[5050:] + B[:5050]
ans = -1

D = [0] * N
L = [i-1 for i in range(N)]
R = [i+1 for i in range(N)]

def calc():
    i = 0
    c = 0
    ans = 0
    while c < N:
        while i < N:
            if B[i] == A[i]:
                if D[i] == 0:
                    c += 1
                    D[i] = 1
                if L[i] >= 0: R[L[i]] = R[i]
                if R[i] < N: L[R[i]] = L[i]
            if B[i] > A[i] and B[i] >= max(B[i-1], B[(i+1)%N]):
                k = - (-(B[i] - max(B[i-1], B[(i+1)%N], A[i])) // (B[i-1] + B[(i+1)%N]))
                B[i] -= (B[i-1] + B[(i+1)%N]) * k
                ans += k
                if k <= 0:
                    return -1
            i = R[i]
        i = N-1
        while i >= 0:
            if B[i] == A[i]:
                if D[i] == 0:
                    c += 1
                    D[i] = 1
                if L[i] >= 0: R[L[i]] = R[i]
                if R[i] < N: L[R[i]] = L[i]
            if B[i] > A[i] and B[i] >= max(B[i-1], B[(i+1)%N]):
                k = - (-(B[i] - max(B[i-1], B[(i+1)%N], A[i])) // (B[i-1] + B[(i+1)%N]))
                B[i] -= (B[i-1] + B[(i+1)%N]) * k
                ans += k
                if k <= 0:
                    return -1
            i = L[i]
    return ans

print((calc()))
# Original solution
# coding: utf-8
# Your code here!

import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline #文字列入力のときは注意

n = int(eval(input()))
a = [int(i) for i in readline().split()]
b = [int(i) for i in readline().split()]


def op(b,i):
    global ans
    if i == n-1:
        res = (b[i]-a[i])//(b[i-1]+b[0])
        b[i] -= (b[i-1]+b[0])*res
    else:
        res = (b[i]-a[i])//(b[i-1]+b[(i+1)])
        b[i] -= (b[i-1]+b[(i+1)])*res
    ans += res
    if res >= 0:
        return res
    else: return 0

def check(b,c):#c からチェック 操作ができる最初のi
#    print(b,c)
    for i in range(c,c+n):
        if op(b,i%n) > 0: return i
    else:
        return(-1)

def rev(b,i):#iから逆向きに操作
    for j in range(i-1,-n,-1):
#        print(b,j)
        if op(b,j%n) == 0: break

    
c = 0
ans = 0
while True:
    i = check(b,c)
    if i == -1: break
    rev(b,i)
    c += 1
    if c >= n: c -= n


if a == b:
    print(ans)
else:
    print((-1))












# Optimized solution
# coding: utf-8
# Your code here!

import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline #文字列入力のときは注意

n = int(eval(input()))
a = [int(i) for i in readline().split()]
b = [int(i) for i in readline().split()]


def op(b,i):
    global ans
    if i == n-1:
        res = (b[i]-a[i])//(b[i-1]+b[0])
        if res < 0: return 0
        b[i] -= (b[i-1]+b[0])*res
    else:
        res = (b[i]-a[i])//(b[i-1]+b[(i+1)])
        if res < 0: return 0
        b[i] -= (b[i-1]+b[(i+1)])*res
    ans += res
    return res

def check(b,c):#c からチェック 操作ができる最初のi
#    print(b,c)
    for i in range(c,c+n):
        if op(b,i%n) > 0: return i
    else:
        return(-1)

def rev(b,i):#iから逆向きに操作
    for j in range(i-1,-n,-1):
#        print(b,j)
        if op(b,j%n) == 0: break

    
c = 0
ans = 0
"""
while True:
    i = check(b,c)
    if i == -1: break
    rev(b,i)
    c += 1
    if c >= n: c -= n
"""

s = set(range(n))

while s:
    i = s.pop()
    if op(b,i) > 0:
        s.add((i-1)%n)
        s.add((i+1)%n)

if a == b:
    print(ans)
else:
    print((-1))