Buggy python submission (verdict: Runtime Error):
N,*L = map(int, open(0).read().split())
A = L[:N]
B = L[N:]
q = []
for i in range(N):
  x,y,z = (i-1)%N,i%N,(i+1)%N
  if A[y]>=A[x]+A[z]>0:
    q.append(y)
a = 0
while q:
  y = q.pop()
  x,z = (y-1)%N,(y+1)%N
  a += A[y]//(A[x]+A[z])
  A[y] = A[y]%(A[x]+A[z])
  s = (x-1)%N
  if A[x]>=A[s]+A[y]>0:
    q.append(x)
  t = (z+1)%N
  if A[z]>=A[y]+A[t]>0:
    q.append(z)
q = []
for i in range(N):
  x,y,z = (i-1)%N,i%N,(i+1)%N
  if B[y]>=B[x]+B[z]>0:
    q.append(y)
b = 0
while q:
  y = q.pop()
  x,z = (y-1)%N,(y+1)%N
  b += B[y]//(B[x]+B[z])
  B[y] = B[y]%(B[x]+B[z])
  s = (x-1)%N
  if B[x]>=B[s]+B[y]>0:
    q.append(x)
  t = (z+1)%N
  if B[z]>=B[y]+B[t]>0:
    q.append(z)
if A==B:
  print(b-a)
else:
  print(-1)


Fixed submission:
from collections import deque
N,*L = map(int, open(0).read().split())
A = L[:N]
B = L[N:]
q = deque()
for i in range(N):
  x,y,z = (i-1)%N,i%N,(i+1)%N
  if A[y]>=A[x]+A[z]>0:
    q.append(y)
a = 0
while q:
  y = q.pop()
  x,z = (y-1)%N,(y+1)%N
  if A[x]+A[z]==0:
    continue
  a += A[y]//(A[x]+A[z])
  A[y] = A[y]%(A[x]+A[z])
  s = (x-1)%N
  if A[x]>=A[s]+A[y]:
    q.append(x)
  t = (z+1)%N
  if A[z]>=A[y]+A[t]:
    q.append(z)
q = deque()
for i in range(N):
  x,y,z = (i-1)%N,i%N,(i+1)%N
  if B[y]>=B[x]+B[z]>0:
    q.append(y)
b = 0
while q:
  y = q.pop()
  x,z = (y-1)%N,(y+1)%N
  if B[x]+B[z]==0:
    continue
  b += B[y]//(B[x]+B[z])
  B[y] = B[y]%(B[x]+B[z])
  s = (x-1)%N
  if B[x]>=B[s]+B[y]:
    q.append(x)
  t = (z+1)%N
  if B[z]>=B[y]+B[t]:
    q.append(z)
if A==B and b>=a:
  print(b-a)
else:
  print(-1)

Buggy python submission (verdict: Runtime Error):
N,*L = map(int, open(0).read().split())
A = L[:N]
B = L[N:]
q = []
for i in range(N):
  x,y,z = (i-1)%N,i%N,(i+1)%N
  if A[y]>=A[x]+A[z]:
    q.append(y)
a = 0
while q:
  y = q.pop()
  x,z = (y-1)%N,(y+1)%N
  a += A[y]//(A[x]+A[z])
  A[y] = A[y]%(A[x]+A[z])
  s = (x-1)%N
  if A[x]>=A[s]+A[y]>0:
    q.append(x)
  t = (z+1)%N
  if A[z]>=A[y]+A[t]>0:
    q.append(z)
q = []
for i in range(N):
  x,y,z = (i-1)%N,i%N,(i+1)%N
  if B[y]>=B[x]+B[z]:
    q.append(y)
b = 0
while q:
  y = q.pop()
  x,z = (y-1)%N,(y+1)%N
  b += B[y]//(B[x]+B[z])
  B[y] = B[y]%(B[x]+B[z])
  s = (x-1)%N
  if B[x]>=B[s]+B[y]>0:
    q.append(x)
  t = (z+1)%N
  if B[z]>=B[y]+B[t]>0:
    q.append(z)
if A==B:
  print(b-a)
else:
  print(-1)

Fixed submission:
from collections import deque
N,*L = map(int, open(0).read().split())
A = L[:N]
B = L[N:]
q = deque()
for i in range(N):
  x,y,z = (i-1)%N,i%N,(i+1)%N
  if A[y]>=A[x]+A[z]>0:
    q.append(y)
a = 0
while q:
  y = q.pop()
  x,z = (y-1)%N,(y+1)%N
  if A[x]+A[z]==0:
    continue
  a += A[y]//(A[x]+A[z])
  A[y] = A[y]%(A[x]+A[z])
  s = (x-1)%N
  if A[x]>=A[s]+A[y]:
    q.append(x)
  t = (z+1)%N
  if A[z]>=A[y]+A[t]:
    q.append(z)
q = deque()
for i in range(N):
  x,y,z = (i-1)%N,i%N,(i+1)%N
  if B[y]>=B[x]+B[z]>0:
    q.append(y)
b = 0
while q:
  y = q.pop()
  x,z = (y-1)%N,(y+1)%N
  if B[x]+B[z]==0:
    continue
  b += B[y]//(B[x]+B[z])
  B[y] = B[y]%(B[x]+B[z])
  s = (x-1)%N
  if B[x]>=B[s]+B[y]:
    q.append(x)
  t = (z+1)%N
  if B[z]>=B[y]+B[t]:
    q.append(z)
if A==B and b>=a:
  print(b-a)
else:
  print(-1)