Buggy python submission (verdict: Time Limit Exceeded):
n = int(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)

Fixed submission:
#import sys
#input = sys.stdin.readline

n = int(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)

Buggy python submission (verdict: Time Limit Exceeded):
#import sys
#input = sys.stdin.readline
 
n = int(input())
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)

Fixed submission:
#import sys
#input = sys.stdin.readline

n = int(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)

Buggy python submission (verdict: Time Limit Exceeded):
#import sys
#input = sys.stdin.readline
 
n = int(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)

Fixed submission:
#import sys
#input = sys.stdin.readline

n = int(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)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

n = int(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)

Fixed submission:
#import sys
#input = sys.stdin.readline

n = int(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)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

n = int(input())
a =list(map(int,input().split()))
b =list(map(int,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)

Fixed submission:
#import sys
#input = sys.stdin.readline

n = int(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)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a =list(map(int,input().split()))
b =list(map(int,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)

Fixed submission:
#import sys
#input = sys.stdin.readline

n = int(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)