CodeEff/ECCO huggingface dataset:
# 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()))
pq = []
for i, b in enumerate(B):
    if A[i] != b:
        heappush(pq, (-b, i))
ans = 0
while pq:
    _, i = heappop(pq)
    B[i] -= B[(i-1) % N] + B[(i+1) % N]
    if B[i] < A[i]:
        ans = -1
        break
    if B[i] > A[i]:
        heappush(pq, (-B[i], i))
    ans += 1
print(ans)

# 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()))
pq = []
for i, b in enumerate(B):
    if A[i] != b:
        heappush(pq, (-b, i))
ans = 0
while pq:
    _, i = heappop(pq)
    s = B[(i-1) % N] + B[(i+1) % N]
    q, r = divmod(B[i] - A[i], s)
    B[i] = A[i] + r
    ans += q
    if q == 0:
        ans = -1
        break
    if B[i] > A[i]:
        heappush(pq, (-B[i], i))
print(ans)
# Original solution
from collections import deque

def resolve():
    n = int(eval(input()))
    aa = list(map(int, input().split()))
    bb = list(map(int, input().split()))

    q = deque()
    diffs = set()

    for i in range(n):
        if bb[i] != aa[i]:
            diffs.add(i)

        bef_i = i-1 if i > 0 else n-1
        aft_i = i+1 if i < n-1 else 0
        if bb[i] > aa[i] and bb[i] > bb[bef_i] + bb[aft_i]:
            q.append(i)

    count = 0

    while len(q) > 0:
        i = q.popleft()

        bef_i = i-1 if i > 0 else n-1
        bef2_i = bef_i-1 if bef_i > 0 else n-1
        aft_i = i+1 if i < n-1 else 0
        aft2_i = aft_i+1 if aft_i < n-1 else 0

        while bb[i] > aa[i] and bb[i] > bb[bef_i] + bb[aft_i]:
            bb[i] = bb[i] - bb[bef_i] - bb[aft_i]
            count += 1

        if bb[i] == aa[i]:
            diffs.remove(i)

        if bb[bef_i] > aa[bef_i] and bb[bef_i] > bb[bef2_i] + bb[i]:
            q.append(bef_i)

        if bb[aft_i] > aa[aft_i] and bb[aft_i] > bb[aft2_i] + bb[i]:
            q.append(aft_i)

    if len(diffs) > 0:
        print((-1))
        return

    print(count)
resolve()

# Optimized solution
from collections import deque

def resolve():
    n = int(eval(input()))
    aa = list(map(int, input().split()))
    bb = list(map(int, input().split()))

    q = deque()
    diffs = set()

    for i in range(n):
        if bb[i] != aa[i]:
            diffs.add(i)

        bef_i = i-1 if i > 0 else n-1
        aft_i = i+1 if i < n-1 else 0
        if bb[i] > aa[i] and bb[i] > bb[bef_i] + bb[aft_i]:
            q.append(i)

    count = 0

    while len(q) > 0:
        i = q.popleft()

        bef_i = i-1 if i > 0 else n-1
        bef2_i = bef_i-1 if bef_i > 0 else n-1
        aft_i = i+1 if i < n-1 else 0
        aft2_i = aft_i+1 if aft_i < n-1 else 0

        count += (bb[i] - aa[i]) // (bb[bef_i] + bb[aft_i])
        bb[i] = (bb[i] - aa[i]) % (bb[bef_i] + bb[aft_i]) + aa[i]

        if bb[i] == aa[i]:
            diffs.remove(i)

        if bb[bef_i] > aa[bef_i] and bb[bef_i] > bb[bef2_i] + bb[i]:
            q.append(bef_i)

        if bb[aft_i] > aa[aft_i] and bb[aft_i] > bb[aft2_i] + bb[i]:
            q.append(aft_i)

    if len(diffs) > 0:
        print((-1))
        return

    print(count)
resolve()
# Original solution
import heapq

n=int(eval(input()))
arr1=list(map(int,input().split()))
arr2=list(map(int,input().split()))
q=[]
for i in range(n):
  if arr2[i]!=arr1[i]:
    heapq.heappush(q,(-arr2[i],i))
cnt=0
while q:
  val,pos=heapq.heappop(q)
  val*=-1
  diff=arr2[pos-1]+arr2[(pos+1)%n]
  move,tmp=divmod(val-arr1[pos],diff)
  if move==0:
    break
  cnt+=move
  tmp+=arr1[pos]
  arr2[pos]=tmp
  if tmp!=arr1[pos]:
    heapq.heappush(q,(-tmp,pos))
for i in range(n):
  if arr1[i]!=arr2[i]:
    print((-1))
    break
else:
  print(cnt)

# Optimized solution
import heapq

n=int(eval(input()))
arr1=list(map(int,input().split()))
arr2=list(map(int,input().split()))
q=[]
for i in range(n):
  if arr2[i]!=arr1[i]:
    heapq.heappush(q,(-arr2[i],i))
cnt=0
while q:
  val,pos=heapq.heappop(q)
  val*=-1
  diff=arr2[pos-1]+arr2[(pos+1)%n]
  move=(val-arr1[pos])//diff
  if move==0:
    break
  cnt+=move
  tmp=arr2[pos]-diff*move
  arr2[pos]=tmp
  if tmp!=arr1[pos]:
    heapq.heappush(q,(-tmp,pos))
for i in range(n):
  if arr1[i]!=arr2[i]:
    print((-1))
    break
else:
  print(cnt)
# Original solution
import math
#import numpy as np
import queue
from collections import deque,defaultdict
import heapq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)

def main():
    n = int(ipt())
    a = [int(i) for i in ipt().split()]
    b = [int(i) for i in ipt().split()]
    hq = []
    ma = max(a)
    ans = 0
    for i,bi in enumerate(b):
        heapq.heappush(hq,(-bi,i))
    while len(hq) > 0:
        nbi,i = heapq.heappop(hq)
        if len(hq) > 0:
            nnbi,ni = heapq.heappop(hq)
            heapq.heappush(hq,(nnbi,ni))
        else:
            nnbi = -ma
        if i < n-1:
            bp = b[i+1]
        else:
            bp = b[0]
        bm = b[i-1]
        if -nbi-bm-bp < a[i]:
            print((-1))
            exit()
        else:
            t = math.ceil((nnbi-nbi)/(bm+bp))
            b[i] -= (bm+bp)*t
            if b[i] > a[i]:
                heapq.heappush(hq,(-b[i],i))
            ans += t
    print(ans)
    return

if __name__ == '__main__':
    main()


# Optimized solution
import math
#import numpy as np
import queue
from collections import deque,defaultdict
import heapq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)

def main():
    n = int(ipt())
    a = [int(i) for i in ipt().split()]
    b = [int(i) for i in ipt().split()]
    hq = []
    ans = 0
    for i,bi in enumerate(b):
        if bi > a[i]:
            heapq.heappush(hq,(-bi,i))
    while len(hq) > 0:
        #print(hq,b)
        nbi,i = heapq.heappop(hq)
        if i == n-1:
            bp = b[0]
        else:
            bp = b[i+1]
        bm = b[i-1]
        t = (-a[i]-nbi)//(bm+bp)
        if t == 0:
            print((-1))
            exit()
        b[i] -= (bm+bp)*t
        if b[i] > a[i]:
            heapq.heappush(hq,(-b[i],i))
        ans += t
    print(ans)
    return

if __name__ == '__main__':
    main()
# Original solution
#!/usr/bin/env python3
#AGC37 C

import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop,heapify
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
a = LI()
b = LI()
h = []
for i,v in enumerate(b):
    heappush(h,(-v,i))
ans = 0
while h:
    v,i = heappop(h)
    v = -v
    tmp = b[(i+1) % n] + b[i-1]
    if v == a[i]:
        continue
    s = (v - a[i])//tmp

    if s <= 0:
        print((-1))
        quit()

    ans += s
    v -= s*tmp
    b[i] = v
    if v != a[i]:
        heappush(h,(-v,i))
print(ans)


# Optimized solution
#!/usr/bin/env python3
#AGC37 C

import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop,heapify
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
a = LI()
b = LI()
h = []
for i in range(n):
    heappush(h,(-b[i],i))
ans = 0
while h:
    v,i = heappop(h)
    v = -v
    tmp = b[(i+1) % n] + b[i-1]
    if v == a[i]:
        continue
    s = (v - a[i])//tmp

    if s <= 0:
        print((-1))
        quit()

    ans += s
    v -= s*tmp
    b[i] = v
    if v != a[i]:
        heappush(h,(-v,i))
print(ans)
# Original solution
def main():
    from sys import exit
    n = int(eval(input()))
    a = [int(e) for e in input().split()]
    b = [int(e) for e in input().split()]
    result = 0
    q = [i for i in range(n) if b[i] != a[i]]
    while len(q) != 0:
        nq = []
        c = 0
        for i in q:
            if i == 0 or i == n - 1:
                j = b[(n + i - 1) % n] + b[(n + i + 1) % n]
            else:
                j = b[i - 1] + b[i + 1]
            if j > b[i] - a[i]:
                nq.append(i)
                continue
            c += 1
            k = (b[i] - a[i]) // j
            result += k
            b[i] -= j * k
            if a[i] != b[i]:
                nq.append(i)
        if c == 0 and len(nq) != 0:
            print((-1))
            exit()
        q = nq
    print(result)


main()


# Optimized solution
def main():
    from sys import exit

    N = int(eval(input()))
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))

    result = 0
    q = [i for i in range(N) if B[i] != A[i]]
    while len(q) != 0:
        nq = []
        c = 0
        for i in q:
            if i == 0 or i == N - 1:
                j = B[(N + i - 1) % N] + B[(N + i + 1) % N]
            else:
                j = B[i - 1] + B[i + 1]
            if j > B[i] - A[i]:
                nq.append(i)
                continue
            c += 1
            k = (B[i] - A[i]) // j
            result += k
            B[i] -= j * k
            if A[i] != B[i]:
                nq.append(i)
        if c == 0 and len(nq) != 0:
            print((-1))
            exit()
        q = nq
    print(result)


main()
# Original solution
n = int(eval(input()))
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
 
cnt, prev_cnt = 0, -1
while cnt != prev_cnt:
    prev_cnt = cnt
    for i in range(n):
        
        adj_sum = b[i - 1] + b[(i + 1) % n]
        k = (b[i] - a[i]) // adj_sum
        if k > 0:
            b[i] -= k * adj_sum
            cnt += k
print((cnt if a == b else -1))

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

n = int(eval(input()))
a = [0]*n
b = [0]*n
a =[int(xi) for xi in input().split()]
b =[int(yi) for yi in input().split()]

cnt, prev_cnt = 0, -1
while cnt != prev_cnt:
    prev_cnt = cnt
    for i in range(n):
        
        adj_sum = b[i - 1] + b[(i + 1) % n]
        k = (b[i] - a[i]) // adj_sum
        if k > 0:
            b[i] -= k * adj_sum
            cnt += k

print((cnt if a == b else -1))
# Original solution
import sys
from heapq import heappop as hpp, heappush as hp

readline = sys.stdin.readline
N = int(readline())
A = list(map(int, readline().split()))
B = list(map(int, readline().split()))

geta = 10**6        
Q = [-(B[i]*geta+i) for i in range(N)] + [0]
Q.sort()

ans = 0
cnt = 0
inf = 10**9+7
while cnt < N:
    b, idx = divmod(-hpp(Q), geta)
    bsec = Q[0]
    bsec = (-bsec)//geta
    a = A[idx]
    if b < a:
        ans = -1
        break
    bside = B[(idx+1)%N] + B[(idx-1)%N]
    kb = (1+(b-bsec)//bside)
    ka = (b-a)//bside
    k = min(ka, kb)
    B[idx] -= k*bside
    ans += k
    if B[idx] == a:
        cnt += 1
    elif k == 0:
        ans = -1
        break
    else:
        hp(Q, -(B[idx]*geta+idx))
print(ans)


# Optimized solution
import sys
from heapq import heappop as hpp, heappush as hp

readline = sys.stdin.readline
N = int(readline())
A = list(map(int, readline().split()))
B = list(map(int, readline().split()))

geta = 10**6        
Q = [-(B[i]*geta+i) for i in range(N)] + [0]
Q.sort()

ans = 0
cnt = 0
inf = 10**9+7
while cnt < N:
    b, idx = divmod(-hpp(Q), geta)
    a = A[idx]
    if b < a:
        ans = -1
        break
    bside = B[(idx+1)%N] + B[(idx-1)%N]
    kb = 1+(b-bside)//bside
    ka = (b-a)//bside
    k = min(ka, kb)
    B[idx] -= k*bside
    ans += k
    if B[idx] == a:
        cnt += 1
    elif k == 0:
        ans = -1
        break
    else:
        hp(Q, -(B[idx]*geta+idx))
print(ans)
# Original solution
import sys
import heapq

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

    h=[]
    for i in range(n):
        if b[i]>a[i]:
            heapq.heappush(h,[b[i]*-1,i])

    loop=0
    idx=0
    while(1):
    #print(b)
        if not h:
            break
        buf,idx=heapq.heappop(h)
        buf=-1*buf

        x=(buf-a[idx])//(b[(idx-1)%n]+b[(idx+1)%n])
        buf-=x*(b[(idx-1)%n]+b[(idx+1)%n])
        loop+=x
        b[idx]=buf
        if x==0:
            #print(b)
            print((-1))
            sys.exit()
        elif buf>a[idx]:
            heapq.heappush(h,[buf*-1,idx])

    print(loop)

if __name__ == '__main__':
    main()

# Optimized solution
import sys
import heapq

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

    h=[]
    for i in range(n):
        if b[i]>a[i]:
            heapq.heappush(h,(b[i]*-1,i))

    loop=0
    idx=0
    while(1):
    #print(b)
        if not h:
            break
        buf,idx=heapq.heappop(h)
        buf=-1*buf

        x=(buf-a[idx])//(b[(idx-1)%n]+b[(idx+1)%n])
        buf-=x*(b[(idx-1)%n]+b[(idx+1)%n])
        loop+=x
        b[idx]=buf
        if x==0:
            #print(b)
            print((-1))
            sys.exit()
        elif buf>a[idx]:
            heapq.heappush(h,(buf*-1,idx))

    print(loop)

if __name__ == '__main__':
    main()
# Original solution
N=int(eval(input()))
A=tuple(map(int,input().split()))
B=list(map(int,input().split()))
from heapq import heappop,heappush
q=[]
a=0
for i,b in enumerate(B):
    heappush(q,(-b,i))
while len(q)>0:
    b,i=heappop(q)
    ba=-b-A[i]
    bb=B[i-1]+B[i+1-N]
    if ba<0:
        a=-1
        break
    elif ba==0:
        continue
    elif -b>bb:
        x=ba//bb
        if x==0:
            a=-1
            break
        B[i]-=bb*x
        heappush(q,(-B[i],i))
        a+=x
    else:
        a=-1
        break
print(a)

# Optimized solution
N=int(eval(input()))
A=tuple(map(int,input().split()))
B=list(map(int,input().split()))
from heapq import heappop,heappush
q=[]
a=0
for i,b in enumerate(B):
    heappush(q,(-b,i))
while len(q)>0:
    b,i=heappop(q)
    ba=-b-A[i]
    bb=B[i-1]+B[i+1-N]
    if ba<0:
        a=-1
        break
    elif ba==0:
        continue
    elif -b>bb:
        x,y=divmod(ba,bb)
        if x==0:
            a=-1
            break
        B[i]-=bb*x
        if y>0:
            heappush(q,(-B[i],i))
        a+=x
    else:
        a=-1
        break
print(a)
# Original solution
from heapq import heappush, heappop

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

def solve() :
    h = []
    for i in range(N) :
        if A[i] == B[i] :
            continue
        heappush(h, (-B[i], i))

    ret = 0
    while len(h) > 1 :
        _, i = heappop(h)
        
        if B[i] > A[i] :
            m, M = B[(i-1)%N], B[(i+1)%N]
            if m > M :
                m, M = M, m
            
            x = (B[i] - B[h[0][1]] + m + M - 1) // (m + M)
            B[i] -= x * (m + M)
            ret += x
        
        if A[i] > B[i] :
            return -1
        elif A[i] < B[i] :
            heappush(h, (-B[i], i))
    
    _, i = heappop(h)
    if (B[i] - A[i]) % (B[(i-1)%N] + B[(i+1)%N]) == 0 :
        ret += (B[i] - A[i]) // (B[(i-1)%N] + B[(i+1)%N])
    else :
        return -1
    
    return ret
    
print((solve()))

# Optimized solution
from heapq import heappush, heappop

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

def solve() :
    h = []
    for i in range(N) :
        if A[i] == B[i] :
            continue
        heappush(h, (-B[i], i))

    ret = 0
    while h :
        _, i = heappop(h)
       
        m, M = B[(i-1)%N], B[(i+1)%N]
        if m > M :
            m, M = M, m
        
        if M >= A[i] :
            x = (B[i] + m - 1) // (m + M)
            B[i] -= x * (m + M)
            ret += x
        else :
            if (B[i] - A[i]) % (m + M) != 0 :
                return -1
            ret += (B[i] - A[i]) // (m + M)
            B[i] = A[i]
        
        if A[i] > B[i] :
            return -1
            
        elif A[i] < B[i] :
            heappush(h, (-B[i], i))
    
    return ret
    
print((solve()))
# Original solution
n=int(eval(input()))
A=list(map(int,input().split( )))
B=list(map(int,input().split( )))
count=0
prv_count=-1#人の見て追加

#flag=True
while count!=prv_count:
    prv_count=count
    for i in range(-1,n-1):
        tonari=B[i-1]+B[i+1]
        tmp=(B[i]-A[i])//tonari
        if tmp>0:
            B[i]-=tonari*tmp
            count+=tmp
            #flag=True
if A==B:
    print(count)
else:
    print((-1))


# Optimized solution
###test
n=int(eval(input()))
A=list(map(int,input().split( )))
B=list(map(int,input().split( )))
count=0
flag=True
while flag:
    flag=False
    for i in range(-1,n-1):
        tonari=B[i-1]+B[i+1]
        tmp=(B[i]-A[i])//tonari
        if tmp>0:
            B[i]-=tonari*tmp
            count+=tmp
            flag=True
if A==B:
    print(count)
else:
    print((-1))
# Original solution
###test
n=int(eval(input()))
A=list(map(int,input().split( )))
B=list(map(int,input().split( )))
count=0
flag=True
while flag:
    flag=False
    for i in range(-1,n-1):
        tonari=B[i-1]+B[i+1]
        tmp=(B[i]-A[i])//tonari
        if tmp>0:
            B[i]-=tonari*tmp
            count+=tmp
            flag=True
if A==B:
    print(count)
else:
    print((-1))


# Optimized solution
n=int(eval(input()))
A=list(map(int,input().split( )))
B=list(map(int,input().split( )))
count=0
flag=True
while flag:
    flag=False
    for i in range(-1,n-1):
        tonari=B[i-1]+B[i+1]
        tmp=(B[i]-A[i])//tonari
        if tmp>0:
            B[i]-=tonari*tmp
            count+=tmp
            flag=True
if A==B:
    print(count)
else:
    print((-1))
# Original solution
def main():
    from heapq import heappop as hpop
    from heapq import heappush as hpush
    n = int(eval(input()))
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    visit = [False]*n
    h = []
    for i in range(n):
        hpush(h, (-b[i], i))
    cnt = 0
    while h:
        i, j = hpop(h)
        i = -i
        jj = (j+1) % n
        if visit[j]:
            if a[j] != b[j]:
                print((-1))
                return 0
            visit[j-1] = True
            visit[jj] = True
            continue
        temp = i-a[j]
        temp2 = b[j-1]+b[jj]
        sho = temp//temp2
        if sho == 0:
            if temp:
                print((-1))
                return 0
            visit[j-1] = True
            visit[jj] = True
            continue
        cnt += sho
        b[j] -= temp2*sho
        hpush(h, (-b[j], j))
    print(cnt)


main()

# Optimized solution
def main():
    from heapq import heappop as hpop
    from heapq import heappush as hpush
    n = int(eval(input()))
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    h = []
    for i in range(n):
        hpush(h, (-b[i], i))
    cnt = 0
    while h:
        i, j = hpop(h)
        i = -i
        jj = (j+1) % n
        temp = i-a[j]
        temp2 = b[j-1]+b[jj]
        sho = temp//temp2
        if sho == 0:
            if temp:
                print((-1))
                return 0
            continue
        cnt += sho
        b[j] -= temp2*sho
        hpush(h, (-b[j], j))
    print(cnt)
main()
# Original solution
import heapq
q = []
heapq.heapify(q)

n = int(eval(input()))
As = list(map(int, input().split()))
Bs = list(map(int, input().split()))

zeros = [False]*n
Cs = [Bs[(i-1)%n]+Bs[(i+1)%n] for i in range(n)]
Ds = [b-(c+a) for b,c,a in zip(Bs,Cs,As)]
# Dが大きい順に操作していく？
# 初期値
for i,d in enumerate(Ds):
    if d < 0: continue
    heapq.heappush(q,(-d,i))

# print("init",Bs,Cs)
cnt = 0
while len(q) > 0:
    d,i = heapq.heappop(q)
    # 自分と周りの影響を更新
    # print(Bs,Cs,i,"before")
    cnt_ = (Bs[i%n]-As[i%n]) // Cs[i%n]
    Bs[i%n] -= cnt_*Cs[i%n]    
    Cs[(i-1)%n] = Bs[(i-2)%n]+Bs[i%n]
    Cs[(i+1)%n] = Bs[(i+2)%n]+Bs[i%n]
    # print("bef",Bs,Cs,i)
    # 周りとの差
    d_m = Bs[(i-1)%n]-Cs[(i-1)%n]-As[(i-1)%n] 
    d_p = Bs[(i+1)%n]-Cs[(i+1)%n]-As[(i+1)%n]
    # print("pop",d,i,d_m,d_p)
    if d_m >= 0:
        heapq.heappush(q,(-d_m,i-1))
    if d_p >= 0:
        heapq.heappush(q,(-d_p,i+1))        
    # print("debug",Bs,Cs)
    cnt += cnt_
if As == Bs:
    print(cnt)
else:
    print((-1))
    


# Optimized solution
import heapq
q = []
heapq.heapify(q)

n = int(eval(input()))
As = list(map(int, input().split()))
Bs = list(map(int, input().split()))

zeros = [False]*n
Cs = [Bs[(i-1)%n]+Bs[(i+1)%n] for i in range(n)]
# Dが大きい順に操作していく
for i, (b,c,a) in enumerate(zip(Bs,Cs,As)):
    d = b-(c+a) 
    if d < 0: continue
    heapq.heappush(q,(-d,i))

cnt = 0
while len(q) > 0:
    d,i = heapq.heappop(q)
    # 自分と周りの影響を更新
    cnt_ = (Bs[i%n]-As[i%n]) // Cs[i%n]
    Bs[i%n] -= cnt_*Cs[i%n]    
    Cs[(i-1)%n] = Bs[(i-2)%n]+Bs[i%n]
    Cs[(i+1)%n] = Bs[(i+2)%n]+Bs[i%n]
    # 周りとの差
    d_m = Bs[(i-1)%n]-Cs[(i-1)%n]-As[(i-1)%n] 
    d_p = Bs[(i+1)%n]-Cs[(i+1)%n]-As[(i+1)%n]
    if d_m >= 0:
        heapq.heappush(q,(-d_m,i-1))
    if d_p >= 0:
        heapq.heappush(q,(-d_p,i+1))
    cnt += cnt_

if As == Bs:
    print(cnt)
else:
    print((-1))
# Original solution
words = lambda t : list(map(t, input().split()))



n = int(eval(input()))
a = words(int)
b = words(int)

def getNext(i):
    if i == n-1:
        return 0
    else:
        return i+1

def getPrev(i):
    if i == 0:
        return n-1
    else:
        return i-1

import queue
q = queue.Queue()

def verify(i):
    if b[i] != a[i] and b[i] - (b[getNext(i)] + b[getPrev(i)]) >= a[i]:
        return True
    else:
        return False

for i in range(len(b)):
    if b[i] >= a[i] and verify(i):
        q.put(i)

ans = 0
succeed = True
while not q.empty():
    i = q.get()
    ni = getNext(i)
    pi = getPrev(i)
    #print(i, b)
    d = b[ni] + b[pi]
    if b[i] % d == a[i] % d:
        ans += b[i] // d - (a[i] // d)
        b[i] = a[i]
    else:
        ans += b[i] // d
        b[i] %= d
    if b[i] < a[i]:
        succeed = False
        break

    if verify(ni):
        q.put(ni)
    if verify(pi):
        q.put(pi)

for i in range(len(b)):
    if a[i] != b[i]:
        succeed = False
        break

if succeed:
    print(ans)
else:
    print((-1))


# Optimized solution
words = lambda t : list(map(t, input().split()))



n = int(eval(input()))
a = words(int)
b = words(int)

def getNext(i):
    if i == n-1:
        return 0
    else:
        return i+1

def getPrev(i):
    if i == 0:
        return n-1
    else:
        return i-1

from collections import deque
q = deque()

def verify(i):
    if b[i] != a[i] and b[i] - (b[getNext(i)] + b[getPrev(i)]) >= a[i]:
        return True
    else:
        return False

for i in range(len(b)):
    if b[i] >= a[i] and verify(i):
        q.append(i)

ans = 0
succeed = True
while not len(q) == 0:
    i = q.popleft()
    ni = getNext(i)
    pi = getPrev(i)
    #print(i, b)
    d = b[ni] + b[pi]
    if b[i] % d == a[i] % d:
        ans += b[i] // d - (a[i] // d)
        b[i] = a[i]
    else:
        ans += b[i] // d
        b[i] %= d
    if b[i] < a[i]:
        succeed = False
        break
    if verify(ni):
        q.append(ni)
    if verify(pi):
        q.append(pi)

for i in range(len(b)):
    if a[i] != b[i]:
        succeed = False
        break

if succeed:
    print(ans)
else:
    print((-1))