Buggy python submission (verdict: Time Limit Exceeded):
#
import sys
import math
 
# ひとつ入力
n = int(input())
 
# initial state
a = [int(i) for i in input().split()] 
# last state
b = [int(i) for i in input().split()] 
 
answer = 0
answer_p = -1
while answer >= 0:
    answer_p = answer
    max_diff=0
    max_diff_i=-1
    for i in range(n):
        diff = b[i] - a[i]
        if max_diff < diff:
            max_diff = diff
            max_diff_i = i
        if b[i] < 1:
            print(-1)
            exit()
    if max_diff < 0:
        print(-1)
        exit()
    elif max_diff==0:
        for i in range(n):
            if b[i] != a[i]:
                print(-1)
                exit()
        print(answer)
        exit()
    else:
        res = (b[max_diff_i]-a[max_diff_i]) // (b[(max_diff_i+1)%n]+b[(max_diff_i-1)%n])
        answer += res
        b[max_diff_i] -= res * (b[(max_diff_i+1)%n]+b[(max_diff_i-1)%n])
    if answer == answer_p:
        print(-1)
        exit()
#    print(a,b,max_diff,max_diff_i)


Fixed submission:
#
import sys
import math

# ひとつ入力
n = int(input())

# initial state
a = [int(i) for i in input().split()] 
# last state
b = [int(i) for i in input().split()] 

answer = 0
answer_p = -1
while answer >= 0:
    answer_p = answer
    for i in range(n):
        tmp = b[(i-1)%n] + b[(i+1)%n]
        if 0 < tmp < b[i] and a[i] < b[i]:
            res = (b[i]-a[i]) // (b[(i+1)%n]+b[(i-1)%n])
            answer += res
            b[i] -= res * (b[(i+1)%n]+b[(i-1)%n])
        
    if answer == answer_p:
        break
#    print(a,b,answer)

for i in range(n):
    if a[i] != b[i]:
        print(-1)
        exit()
print(answer)

Buggy python submission (verdict: Time Limit Exceeded):
#
import sys
import math

# ひとつ入力
n = int(input())

# initial state
a = [int(i) for i in input().split()] 
# last state
b = [int(i) for i in input().split()] 

answer = 0
answer_p = -1
while answer >= 0:
    answer_p = answer
    for i in range(n):
        tmp = b[(i-1)%n] + b[(i+1)%n]
        if 0 < tmp < b[i] and a[i] < b[i]:
            res = (b[i]-a[i]) // (b[(i+1)%n]+b[(i-1)%n])
            answer += res
            b[i] -= res * (b[(i+1)%n]+b[(i-1)%n])
        
    if answer == answer_p:
        break
#    print(a,b,answer)

for i in range(n):
    if a[i] != b[i]:
        print(-1)
        exit()
print(answer)


Fixed submission:
#
import sys
import math

# ひとつ入力
n = int(input())

# initial state
a = [int(i) for i in input().split()] 
# last state
b = [int(i) for i in input().split()] 

answer = 0
answer_p = -1
while answer >= 0:
    answer_p = answer
    for i in range(n):
        tmp = b[(i-1)%n] + b[(i+1)%n]
        if 0 < tmp < b[i] and a[i] < b[i]:
            res = (b[i]-a[i]) // (b[(i+1)%n]+b[(i-1)%n])
            answer += res
            b[i] -= res * (b[(i+1)%n]+b[(i-1)%n])
        
    if answer == answer_p:
        break
#    print(a,b,answer)

for i in range(n):
    if a[i] != b[i]:
        print(-1)
        exit()
print(answer)

Buggy python submission (verdict: Time Limit Exceeded):
#
import sys
import math

# ひとつ入力
n = int(input())

# initial state
a = [int(i) for i in input().split()] 
# last state
b = [int(i) for i in input().split()] 

answer = 0
answer_p = -1
while answer >= 0:
    answer_p = answer
    max_diff=0
    max_diff_i=-1
    for i in range(n):
        diff = b[i] - a[i]
        if max_diff < diff:
            max_diff = diff
            max_diff_i = i
        if b[i] < 1:
            print(-1)
            exit()
    if max_diff < 0:
        print(-1)
        exit()
    elif max_diff==0:
        for i in range(n):
            if b[i] != a[i]:
                print(-1)
                exit()
        print(answer)
        exit()
    else:
        if max_diff_i==n-1:
            res = (b[n-1]-a[n-1]) // (b[0]+b[n-2])
            answer += res
            b[n-1] -= res * (b[0]+b[n-2])
        else:
            res = (b[max_diff_i]-a[max_diff_i]) // (b[max_diff_i+1]+b[max_diff_i-1])
            answer += res
            b[max_diff_i] -= res * (b[max_diff_i+1]+b[max_diff_i-1])
    if answer == answer_p:
        print(-1)
        exit()
#    print(a,b,max_diff,max_diff_i)




Fixed submission:
#
import sys
import math

# ひとつ入力
n = int(input())

# initial state
a = [int(i) for i in input().split()] 
# last state
b = [int(i) for i in input().split()] 

answer = 0
answer_p = -1
while answer >= 0:
    answer_p = answer
    for i in range(n):
        tmp = b[(i-1)%n] + b[(i+1)%n]
        if 0 < tmp < b[i] and a[i] < b[i]:
            res = (b[i]-a[i]) // (b[(i+1)%n]+b[(i-1)%n])
            answer += res
            b[i] -= res * (b[(i+1)%n]+b[(i-1)%n])
        
    if answer == answer_p:
        break
#    print(a,b,answer)

for i in range(n):
    if a[i] != b[i]:
        print(-1)
        exit()
print(answer)

Buggy python submission (verdict: Time Limit Exceeded):
#
import sys
import math

# ひとつ入力
n = int(input())

# initial state
a = [int(i) for i in input().split()] 
# last state
b = [int(i) for i in input().split()] 

answer = 0
answer_p = -1
while answer >= 0:
    answer_p = answer
    max_diff=0
    max_diff_i=-1
    for i in range(n):
        diff = b[i] - a[i]
        if max_diff < diff:
            max_diff = diff
            max_diff_i = i
        if b[i] < 1:
            print(-1)
            exit()
    if max_diff < 0:
        print(-1)
        exit()
    elif max_diff==0:
        for i in range(n):
            if b[i] != a[i]:
                print(-1)
                exit()
        print(answer)
        exit()
    else:
        if max_diff_i==n-1:
            answer += ( (b[n-1]-a[n-1]) // (b[0]+b[n-2]) )
            b[n-1] = (b[n-1]-a[n-1]) % (b[0]+b[n-2]) +a[n-1]
#            b[n-1] = b[n-1] - b[0] - b[n-2]
        else:
            answer += ( (b[max_diff_i]-a[max_diff_i]) // (b[max_diff_i+1]+b[max_diff_i-1]) )
            b[max_diff_i] = (b[max_diff_i]-a[max_diff_i]) % (b[max_diff_i+1]+b[max_diff_i-1]) + a[max_diff_i]
#            b[max_diff_i] = b[max_diff_i] - b[max_diff_i+1] - b[max_diff_i-1]
    if answer == answer_p:
        print(-1)
        exit()
#    print(a,b,max_diff,max_diff_i)




Fixed submission:
#
import sys
import math

# ひとつ入力
n = int(input())

# initial state
a = [int(i) for i in input().split()] 
# last state
b = [int(i) for i in input().split()] 

answer = 0
answer_p = -1
while answer >= 0:
    answer_p = answer
    for i in range(n):
        tmp = b[(i-1)%n] + b[(i+1)%n]
        if 0 < tmp < b[i] and a[i] < b[i]:
            res = (b[i]-a[i]) // (b[(i+1)%n]+b[(i-1)%n])
            answer += res
            b[i] -= res * (b[(i+1)%n]+b[(i-1)%n])
        
    if answer == answer_p:
        break
#    print(a,b,answer)

for i in range(n):
    if a[i] != b[i]:
        print(-1)
        exit()
print(answer)

Buggy python submission (verdict: Time Limit Exceeded):
#
import sys
import math

# ひとつ入力
n = int(input())

# initial state
a = [int(i) for i in input().split()] 
# last state
b = [int(i) for i in input().split()] 

answer = 0
answer_p = -1
while answer >= 0:
    answer_p = answer
    max_diff=0
    max_diff_i=-1
    for i in range(n):
        diff = b[i] - a[i]
        if max_diff < diff:
            max_diff = diff
            max_diff_i = i
        if b[i] < 1:
            print(-1)
            exit()
    if max_diff < 0:
        print(-1)
        exit()
    elif max_diff==0:
        for i in range(n):
            if b[i] != a[i]:
                print(-1)
                exit()
        print(answer)
        exit()
    else:
        if max_diff_i==n-1:
            answer += ( (b[n-1]-a[n-1]) // (b[0]+b[n-2]) )
            b[n-1] = (b[n-1]-a[n-1]) % (b[0]+b[n-2]) +a[n-1]
#            b[n-1] = b[n-1] - b[0] - b[n-2]
        else:
            answer += ( (b[max_diff_i]-a[max_diff_i]) // (b[max_diff_i+1]+b[max_diff_i-1]) )
            b[max_diff_i] = (b[max_diff_i]-a[max_diff_i]) % (b[max_diff_i+1]+b[max_diff_i-1]) + a[max_diff_i]
#            b[max_diff_i] = b[max_diff_i] - b[max_diff_i+1] - b[max_diff_i-1]
    if answer == answer_p:
        print(-1)
        exit()
#    print(a,b,max_diff,max_diff_i)




Fixed submission:
#
import sys
import math

# ひとつ入力
n = int(input())

# initial state
a = [int(i) for i in input().split()] 
# last state
b = [int(i) for i in input().split()] 

answer = 0
answer_p = -1
while answer >= 0:
    answer_p = answer
    for i in range(n):
        tmp = b[(i-1)%n] + b[(i+1)%n]
        if 0 < tmp < b[i] and a[i] < b[i]:
            res = (b[i]-a[i]) // (b[(i+1)%n]+b[(i-1)%n])
            answer += res
            b[i] -= res * (b[(i+1)%n]+b[(i-1)%n])
        
    if answer == answer_p:
        break
#    print(a,b,answer)

for i in range(n):
    if a[i] != b[i]:
        print(-1)
        exit()
print(answer)

Buggy python submission (verdict: Time Limit Exceeded):
#
import sys
import math
import numpy as np
import itertools

# ひとつ入力
n = int(input())

# initial state
a = [int(i) for i in input().split()] 
# last state
b = [int(i) for i in input().split()] 

answer = 0
while 1:
    max_diff=0
    max_diff_i=-1
    for i in range(n):
        diff = b[i] - a[i]
        if max_diff < diff:
            max_diff = diff
            max_diff_i = i
        if b[i] < 1:
            print("-1")
            exit()
    if max_diff < 0:
        print("-1")
        exit()
    elif max_diff==0:
        for i in range(n):
            if b[i] != a[i]:
                print("-1")
                exit()
        print(answer)
        exit()
    else:
        answer += 1
        if max_diff_i==n-1:
            b[n-1] = b[n-1] - b[0] - b[n-2]
        else:
            b[max_diff_i] = b[max_diff_i] - b[max_diff_i+1] - b[max_diff_i-1]
#    print(a,b,max_diff,max_diff_i)




Fixed submission:
#
import sys
import math

# ひとつ入力
n = int(input())

# initial state
a = [int(i) for i in input().split()] 
# last state
b = [int(i) for i in input().split()] 

answer = 0
answer_p = -1
while answer >= 0:
    answer_p = answer
    for i in range(n):
        tmp = b[(i-1)%n] + b[(i+1)%n]
        if 0 < tmp < b[i] and a[i] < b[i]:
            res = (b[i]-a[i]) // (b[(i+1)%n]+b[(i-1)%n])
            answer += res
            b[i] -= res * (b[(i+1)%n]+b[(i-1)%n])
        
    if answer == answer_p:
        break
#    print(a,b,answer)

for i in range(n):
    if a[i] != b[i]:
        print(-1)
        exit()
print(answer)