CodeEff/ECCO huggingface dataset:
# Original solution
n = int(eval(input()))
seat = [0] * (10 ** 5 + 1)
for i in range(n):
    l, r = list(map(int, input().split()))
    seat[l - 1] += 1
    seat[r] -= 1

for i in range(1, 10 ** 5):
    seat[i] += seat[i - 1]

res = 0
for i in range(10 ** 5):
    if seat[i] == 1:
        res += 1
print(res)


# Optimized solution
n = int(eval(input()))
# seat = [0] * 10 ** 6
seat = [0] * 10 ** 5
for i in range(n):
    l, r = list(map(int, input().split()))
    for j in range(l, r + 1):
        seat[j - 1] = 1

res = 0
# for i in range(10 ** 6):
for i in range(10 ** 5):
    if seat[i] == 1:
        res += 1
print(res)
# Original solution
n = int(eval(input()))
# seat = [0] * 10 ** 6
seat = [0] * 10 ** 5
for i in range(n):
    l, r = list(map(int, input().split()))
    for j in range(l, r + 1):
        seat[j - 1] = 1

res = 0
# for i in range(10 ** 6):
for i in range(10 ** 5):
    if seat[i] == 1:
        res += 1
print(res)


# Optimized solution
n = int(eval(input()))
res = 0
for i in range(n):
    r, l = list(map(int, input().split()))
    res += l - r + 1
print(res)
# Original solution
print((sum(1-eval(s.replace(' ','-'))for s in open(0).readlines()[1:])))

# Optimized solution
_,*t=open(0);print((sum(1-eval(s.replace(' ','-'))for s in t)))
# Original solution
import collections
n = int(eval(input()))
l = [list(map(int, input().split())) for _ in range(n)]
d = {}
for i in range(len(l)):
    for j in range(l[i][0], l[i][1] + 1):
        d[j] = 1
print((len(d)))


# Optimized solution
n = int(eval(input()))
t = 0
for i in range(n):
    l, r = list(map(int, input().split()))
    t += (r - l + 1)
print(t)
# Original solution
n = int(eval(input()))
l = [tuple(map(int, input().split())) for i in range(n)]

cnt = 0
for i in l:
    cnt += (i[1] - i[0] + 1)
print(cnt)


# Optimized solution
N = int(eval(input()))
ans =  0

for i in range(N):
    l,r = list(map(int, input().split()))
    ans += r - l + 1
print(ans)
# Original solution
n = int(eval(input()))
count = 0
for i in range(n):
    l,r = list(map(int,input().split()))
    count += r-l+1
print(count)

# Optimized solution
n = int(eval(input()))
count = 0
for i in range(n):
    li,ri = list(map(int,input().split()))
    count += ri-li+1
print(count)
# Original solution
print((sum(s[1] - s[0] + 1 for s in [list(map(int, input().split())) for _ in range(int(eval(input())))])))


# Optimized solution
import sys
stdin = sys.stdin
def input():
  return stdin.readline().strip()
print((sum(s[1] - s[0] + 1 for s in [list(map(int, input().split())) for _ in range(int(eval(input())))])))
# Original solution
n=int(eval(input()))
c=0
for i in range(n):
    l,r=input().split()
    c+=(int(r)-int(l)+1)

print(c)


# Optimized solution
c=0
n=int(eval(input()))
for i in range(n):
    l,r=list(map(int,input().split()))
    c+=(r-l+1)
print(c)
# Original solution
# import bisect
# import copy
# import fractions
# import math
# import numpy as np
# from collections import Counter, deque
# from itertools import accumulate,permutations, combinations,combinations_with_replacement,product

def resolve():
    N=int(eval(input()))
    cnt=0
    for i in range(N):
        l,r=list(map(int,input().split()))
        cnt+=r-l+1
    print(cnt)


resolve()

# Optimized solution
import sys
sys.setrecursionlimit(10 ** 5 + 10)
def input(): return sys.stdin.readline().strip()

def resolve():
    cnt=0
    N=int(eval(input()))
    for i in range(N):
        l,r=list(map(int,input().split()))
        cnt+=r-l+1
    print(cnt)

resolve()
# Original solution
# 2019-11-12 22:21:08(JST)
import sys
# import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r
import itertools
# from functools import reduce
# import operator as op
# from scipy.misc import comb # float
# import numpy as np 

def main():
    s = sys.stdin.readline().rstrip()
    
    all_words = []
    for i in range(1, len(s) + 1):
        for letters in itertools.product('ATCG', repeat=i):
            word = ''.join(letters)
            all_words.append(word)
    
    for i in range(len(all_words)-1, 0-1, -1):
        if all_words[i] in s:
            print((len(all_words[i])))
            sys.exit()
    print((0))




if __name__ == "__main__":
    main()


# Optimized solution
# 2019-11-12 22:21:08(JST)
import sys
# import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r
# import itertools
# from functools import reduce
# import operator as op
# from scipy.misc import comb # float
# import numpy as np 

def main():
    s = sys.stdin.readline().rstrip()
    
    permitted = 'ACGT'
    continuous_count = []
    count = 0

    for i in range(len(s)):
        char = s[i]
        if char in permitted:
            count += 1
        else:
            continuous_count.append(count)
            count = 0
        if i == len(s) - 1:
            continuous_count.append(count)
    
    if not continuous_count:
        print((len(s)))
    else:
        print((max(continuous_count)))



if __name__ == "__main__":
    main()
# Original solution
#ABC122B
def main():
    import sys
    S = sys.stdin.readline().rstrip()
    #print(S)
    L = ['A', 'C', 'G', 'T']
    cnt, ans = 0, 0
    for s in S:
        if s in L:
            cnt+=1
        else:
            if ans < cnt:
                ans = cnt
            #print(ans)
            cnt=0
    else:
        if ans < cnt:
            ans = cnt
    print(ans)


if __name__=='__main__':
    main()

# Optimized solution
#ABC122B
def main():
    import sys
    S = sys.stdin.readline().rstrip()
    #print(S)
    cnt = 0
    ans = 0
    for s in S:
        if s in ['A', 'G', 'C', 'T']:
            cnt +=1
        else:
            ans = max(ans, cnt)
            cnt=0
    
    ans = max(ans, cnt)
    print(ans)   
            
                    
    

if __name__=='__main__':
    main()
# Original solution
import sys
from collections import deque
from itertools import *


def I(): return int(sys.stdin.readline().rstrip())


def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))


def S(): return sys.stdin.readline().rstrip()


def LS(): return list(sys.stdin.readline().rstrip().split())


s = S()

ans = 0
temp = 0
for s_s in s:
    if s_s in 'ACGT':
        temp += 1
    else:
        temp = 0
    ans = max(ans, temp)
print(ans)


# Optimized solution
import sys

def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

s = S()

temp = 0
ans = 0

for char in s:
    if char in 'ACGT':
        temp += 1
    else:
        temp = 0
    ans = max(ans,temp)
print(ans)
# Original solution
import sys

def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

s = S()

temp = 0
ans = 0

for char in s:
    if char in 'ACGT':
        temp += 1
    else:
        temp = 0
    ans = max(ans,temp)
print(ans)

# Optimized solution
import sys

def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

s = S()
ans = 0
temp = 0
for i in s:
    if i in 'ACGT':
        temp += 1
    else:
        temp = 0
    ans = max(ans,temp)
print(ans)
# Original solution
from collections import deque
N = int(eval(input()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))

# check reachability B -> A ?
# 隣接和table
P = [B[(i-1)%N] + B[(i+1)%N] for i in range(N)]
D = deque(i for i in range(N) if B[i] - P[i] >= A[i])
ans = 0
nmatch = [B[i] == A[i] for i in range(N)].count(True)
while D:
    upd = False
    i = D.popleft()
    if B[i] == A[i]:
        nmatch += 1
        continue
    if B[i] <= P[i]:
        continue
    if B[i] - P[i] >= A[i]:
        P[(i-1)%N] -= P[i]
        P[(i+1)%N] -= P[i]
        B[i] -= P[i]
        upd = True
        ans += 1
        if B[i] == A[i]:
            nmatch += 1
        if B[(i-1)%N] - P[(i-1)%N] >= A[(i-1)%N]:
            D.append((i-1)%N)
        if B[(i+1)%N] - P[(i+1)%N] >= A[(i+1)%N]:
            D.append((i+1)%N)
        if B[i] - P[i] >= A[i]:
            D.append(i)
    if not upd:
        break

print((ans if nmatch == N else -1))


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

# check reachability B -> A ?
# 隣接和table
P = [B[(i-1)%N] + B[(i+1)%N] for i in range(N)]
D = deque(i for i in range(N) if B[i] - P[i] >= A[i])
ans = 0
nmatch = [B[i] == A[i] for i in range(N)].count(True)
while D:
    upd = False
    i = D.popleft()
    if B[i] == A[i]:
        nmatch += 1
        continue
    if B[i] <= P[i]:
        continue
    if B[i] - P[i] >= A[i]:
        n = (B[i] - A[i]) // P[i]
        P[(i-1)%N] -= P[i] * n
        P[(i+1)%N] -= P[i] * n
        B[i] -= P[i] * n
        upd = True
        ans += n
        if B[i] == A[i]:
            nmatch += 1
        if B[(i-1)%N] - P[(i-1)%N] >= A[(i-1)%N]:
            D.append((i-1)%N)
        if B[(i+1)%N] - P[(i+1)%N] >= A[(i+1)%N]:
            D.append((i+1)%N)
    if not upd:
        break

print((ans if nmatch == N else -1))
# Original solution
from pprint import pprint
import queue

def solve(n, a, b):
        
    state = queue.Queue()
    state.put((0, b[:]))
    while not state.empty():
        count, b_tmp = state.get()
        # print("next:" + str(count))
        # pprint(b_tmp)
        for i, b_i in enumerate(b_tmp):
            i_a = (i - 1 + n) % n
            i_c = (i + 1) % n
            b_cand = b_i - b_tmp[i_a] - b_tmp[i_c]
            if b_cand >= a[i]:
                b_next = b_tmp[:]
                b_next[i] = b_cand
                if b_next == a:
                    return count + 1
                # print(count + 1)
                state.put((count + 1, b_next))
    
    return -1


if __name__ == '__main__':

    n = int(input().strip())
    a = list(map(int, input().strip().split(" ")))
    b = list(map(int, input().strip().split(" ")))
    print((solve(n, a, b)))

# Optimized solution
from pprint import pprint
import queue

def solve(n, a, b):
    a_pq = queue.PriorityQueue()
    for i, a_i in enumerate(a):
        a_pq.put((a_i, i))
    b_pq = queue.PriorityQueue()
    for i, b_i in enumerate(b):
        b_pq.put((b_i, i))

    state = queue.Queue()
    state.put((0, b[:]))
    while not state.empty():
        count, b_tmp = state.get()
        b_tmp_pq = queue.PriorityQueue()
        for b_tmp_i in b_tmp:
            b_tmp_pq.put(b_tmp_i)
        # print("next:" + str(count))
        # pprint(b_tmp)
        while not b_tmp_pq.empty():
            b_i = b_tmp_pq.get()
            i_b = b_tmp.index(b_i)
            # print(i_b, b_i)
            i_a = (i_b - 1 + n) % n
            i_c = (i_b + 1) % n
            b_cand = b_i
            if b_cand - b_tmp[i_a] - b_tmp[i_c] < a[i_b]:
                continue

            while b_cand - b_tmp[i_a] - b_tmp[i_c] >= a[i_b]:
                b_cand = b_cand - b_tmp[i_a] - b_tmp[i_c]
                count += 1

            b_next = b_tmp[:]
            b_next[i_b] = b_cand
            if b_next == a:
                return count
            # print(count + 1)
            state.put((count, b_next))
    
    return -1


if __name__ == '__main__':

    n = int(input().strip())
    a = list(map(int, input().strip().split(" ")))
    b = list(map(int, input().strip().split(" ")))
    print((solve(n, a, b)))
# Original solution
from pprint import pprint
import queue

def solve(n, a, b):
    a_pq = queue.PriorityQueue()
    for i, a_i in enumerate(a):
        a_pq.put((a_i, i))
    b_pq = queue.PriorityQueue()
    for i, b_i in enumerate(b):
        b_pq.put((b_i, i))

    state = queue.Queue()
    state.put((0, b[:]))
    while not state.empty():
        count, b_tmp = state.get()
        b_tmp_pq = queue.PriorityQueue()
        for b_tmp_i in b_tmp:
            b_tmp_pq.put(b_tmp_i)
        # print("next:" + str(count))
        # pprint(b_tmp)
        while not b_tmp_pq.empty():
            b_i = b_tmp_pq.get()
            i_b = b_tmp.index(b_i)
            # print(i_b, b_i)
            i_a = (i_b - 1 + n) % n
            i_c = (i_b + 1) % n
            b_cand = b_i
            if b_cand - b_tmp[i_a] - b_tmp[i_c] < a[i_b]:
                continue

            while b_cand - b_tmp[i_a] - b_tmp[i_c] >= a[i_b]:
                b_cand = b_cand - b_tmp[i_a] - b_tmp[i_c]
                count += 1

            b_next = b_tmp[:]
            b_next[i_b] = b_cand
            if b_next == a:
                return count
            # print(count + 1)
            state.put((count, b_next))
    
    return -1


if __name__ == '__main__':

    n = int(input().strip())
    a = list(map(int, input().strip().split(" ")))
    b = list(map(int, input().strip().split(" ")))
    print((solve(n, a, b)))

# Optimized solution
from pprint import pprint
import queue

def solve(n, a, b):
    a_pq = queue.PriorityQueue()
    for i, a_i in enumerate(a):
        a_pq.put((a_i, i))
    b_pq = queue.PriorityQueue()
    for i, b_i in enumerate(b):
        b_pq.put((b_i, i))

    state = queue.Queue()
    state.put((0, b[:]))
    while not state.empty():
        count, b_tmp = state.get()
        b_tmp_pq = queue.PriorityQueue()
        for b_tmp_i in b_tmp:
            b_tmp_pq.put(b_tmp_i)
        # print("next:" + str(count))
        # pprint(b_tmp)
        while not b_tmp_pq.empty():
            b_i = b_tmp_pq.get()
            i_b = b_tmp.index(b_i)
            # print(i_b, b_i)
            i_a = (i_b - 1 + n) % n
            i_c = (i_b + 1) % n
            b_cand = b_i
            if b_cand - b_tmp[i_a] - b_tmp[i_c] < a[i_b]:
                continue

            while b_cand - b_tmp[i_a] - b_tmp[i_c] >= a[i_b]:
                b_cand = b_cand - b_tmp[i_a] - b_tmp[i_c]
                count += 1

            b_next = b_tmp[:]
            b_next[i_b] = b_cand
            if b_next == a:
                return count
            # print(count + 1)
            state.put((count, b_next))
            break
    
    return -1


if __name__ == '__main__':

    n = int(input().strip())
    a = list(map(int, input().strip().split(" ")))
    b = list(map(int, input().strip().split(" ")))
    print((solve(n, a, b)))
# Original solution
from pprint import pprint
import heapq

def solve(n, a, b):
    b_pq = []
    b_rev = {}
    for i, b_i in enumerate(b):
        heapq.heappush(b_pq, (-1 * b_i, i))

    count = 0
    while b_pq:
        tmp = heapq.heappop(b_pq)
        # pprint(tmp)
        b_i = tmp[0] * -1
        i_b = tmp[1]
        # print(i_b, b_i)
        i_a = (i_b - 1 + n) % n
        i_c = (i_b + 1) % n
        b_cand = b_i
        if b_cand - b[i_a] - b[i_c] < a[i_b]:
            continue

        #while b_cand - b[i_a] - b[i_c] >= a[i_b]:
        #    b_cand = b_cand - b[i_a] - b[i_c]
        #    count += 1
        tmp_count =  (b_cand - a[i_b]) // (b[i_a] + b[i_c])
        b_cand -= (b[i_a] + b[i_c]) * tmp_count
        count += tmp_count

        b[i_b] = b_cand
        heapq.heappush(b_pq, (-1 * b_cand, i_b))
        # print("count: ", count)
        # pprint(b)
        if b_cand == a[i_b] and b == a:
            return count
    
    return -1


if __name__ == '__main__':

    n = int(input().strip())
    a = list(map(int, input().strip().split(" ")))
    b = list(map(int, input().strip().split(" ")))
    print((solve(n, a, b)))

# Optimized solution
from pprint import pprint
import heapq

def solve(n, a, b):
    b_pq = []
    b_rev = {}
    delete = []
    for i, b_i in enumerate(b):
        if b_i == a[i]:
            continue
        heapq.heappush(b_pq, (-1 * b_i, i))

    count = 0
    while b_pq:
        tmp = heapq.heappop(b_pq)
        # pprint(tmp)
        b_i = tmp[0] * -1
        i_b = tmp[1]
        # print(i_b, b_i)
        i_a = (i_b - 1 + n) % n
        i_c = (i_b + 1) % n
        b_cand = b_i
        if b_cand - b[i_a] - b[i_c] < a[i_b]:
            continue

        #while b_cand - b[i_a] - b[i_c] >= a[i_b]:
        #    b_cand = b_cand - b[i_a] - b[i_c]
        #    count += 1
        tmp_count =  (b_cand - a[i_b]) // (b[i_a] + b[i_c])
        b_cand -= (b[i_a] + b[i_c]) * tmp_count
        count += tmp_count

        b[i_b] = b_cand
        heapq.heappush(b_pq, (-1 * b_cand, i_b))
        # print("count: ", count)
        # pprint(b)
        if b_cand == a[i_b] and b == a:
            return count
    
    return -1


if __name__ == '__main__':

    n = int(input().strip())
    a = list(map(int, input().strip().split(" ")))
    b = list(map(int, input().strip().split(" ")))
    print((solve(n, a, b)))
# Original solution
from pprint import pprint
import heapq

def solve(n, a, b):
    b_pq = []
    b_rev = {}
    delete = []
    for i, b_i in enumerate(b):
        if b_i == a[i]:
            continue
        heapq.heappush(b_pq, (-1 * b_i, i))

    count = 0
    while b_pq:
        tmp = heapq.heappop(b_pq)
        # pprint(tmp)
        b_i = tmp[0] * -1
        i_b = tmp[1]
        # print(i_b, b_i)
        i_a = (i_b - 1 + n) % n
        i_c = (i_b + 1) % n
        b_cand = b_i
        if b_cand - b[i_a] - b[i_c] < a[i_b]:
            continue

        #while b_cand - b[i_a] - b[i_c] >= a[i_b]:
        #    b_cand = b_cand - b[i_a] - b[i_c]
        #    count += 1
        tmp_count =  (b_cand - a[i_b]) // (b[i_a] + b[i_c])
        b_cand -= (b[i_a] + b[i_c]) * tmp_count
        count += tmp_count

        b[i_b] = b_cand
        heapq.heappush(b_pq, (-1 * b_cand, i_b))
        # print("count: ", count)
        # pprint(b)
        if b_cand == a[i_b] and b == a:
            return count
    
    return -1


if __name__ == '__main__':

    n = int(input().strip())
    a = list(map(int, input().strip().split(" ")))
    b = list(map(int, input().strip().split(" ")))
    print((solve(n, a, b)))

# Optimized solution
from pprint import pprint
import heapq

def solve(n, a, b):
    b_pq = []
    b_rev = {}
    delete = []
    for i, b_i in enumerate(b):
        if b_i == a[i]:
            continue
        heapq.heappush(b_pq, (-1 * b_i, i))

    count = 0
    while b_pq:
        tmp = heapq.heappop(b_pq)
        # pprint(tmp)
        b_i = tmp[0] * -1
        i_b = tmp[1]
        # print(i_b, b_i)
        i_a = (i_b - 1 + n) % n
        i_c = (i_b + 1) % n
        b_cand = b_i
        if b_cand - b[i_a] - b[i_c] < a[i_b]:
            continue

        #while b_cand - b[i_a] - b[i_c] >= a[i_b]:
        #    b_cand = b_cand - b[i_a] - b[i_c]
        #    count += 1
        tmp_count =  (b_cand - a[i_b]) // (b[i_a] + b[i_c])
        b_cand -= (b[i_a] + b[i_c]) * tmp_count
        count += tmp_count

        b[i_b] = b_cand
        if b_cand != a[i_b]:
            heapq.heappush(b_pq, (-1 * b_cand, i_b))
        # print("count: ", count)
        # pprint(b)
         

        if b_cand == a[i_b] and b == a:
            return count
    
    return -1


if __name__ == '__main__':

    n = int(input().strip())
    a = list(map(int, input().strip().split(" ")))
    b = list(map(int, input().strip().split(" ")))
    print((solve(n, a, b)))
# Original solution
def main():
	n=int(eval(input()))
#	for i in range(n):
	src = [int(item) for item in input().split()]
	dst = [int(item) for item in input().split()]
	q=set()
	s=set()
	ans=0
	def	SandQ(i):
		ri=i+1
		li=i-1
		if i==0:li=n-1
		if i==n-1:ri=0
		x=0
		if dst[i]-src[i]>=dst[ri]+dst[li]:
			x=(dst[i]-src[i])//(dst[ri]+dst[li])
			dst[i]-=(dst[ri]+dst[li])*x
			q.add(li)
			q.add(ri)
		if dst[i]<=src[i]:
			s.add(i)
#		print (i,dst,x)
		return x;
#	print(src)
#	print(dst)
	for i in range(n):
		ans+=SandQ(i)
	while len(q)>0:
		i=q.pop()
		ans+=SandQ(i)
	if len(s)!=n:print((-1))
	else:print(ans)
#	print(dst)
main()


# Optimized solution
def main():
	n=int(eval(input()))
	src = [int(item) for item in input().split()]
	dst = [int(item) for item in input().split()]
	q=set()
	ans=0
	def	SandQ(i):
		ri=i+1
		li=i-1
		if i==0:li=n-1
		if i==n-1:ri=0
		x=0
		if dst[i]-src[i]>=dst[ri]+dst[li]:
			x=(dst[i]-src[i])//(dst[ri]+dst[li])
			dst[i]-=(dst[ri]+dst[li])*x
			q.add(li)
			q.add(ri)
		return x;
	for i in range(n):
		ans+=SandQ(i)
	while len(q)>0:
		i=q.pop()
		ans+=SandQ(i)
	for i in range(n):
		if src[i]!=dst[i]:
			print((-1))
			exit()
	else:print(ans)
main()
# Original solution
def main():
	n=int(eval(input()))
	src = [int(item) for item in input().split()]
	dst = [int(item) for item in input().split()]
	q=set()
	ans=0
	def	SandQ(i):
		ri=i+1
		li=i-1
		if i==0:li=n-1
		if i==n-1:ri=0
		x=0
		if dst[i]-src[i]>=dst[ri]+dst[li]:
			x=(dst[i]-src[i])//(dst[ri]+dst[li])
			dst[i]-=(dst[ri]+dst[li])*x
			q.add(li)
			q.add(ri)
		return x;
	for i in range(n):
		ans+=SandQ(i)
	while len(q)>0:
		i=q.pop()
		ans+=SandQ(i)
	for i in range(n):
		if src[i]!=dst[i]:
			print((-1))
			exit()
	else:print(ans)
main()


# Optimized solution
def main():
	n=int(eval(input()))
	src = [int(item) for item in input().split()]
	dst = [int(item) for item in input().split()]
	ans=0
	def	SandQ(i):
		ri=i+1
		li=i-1
		if i==0:li=n-1
		if i==n-1:ri=0
		x=0
		if dst[i]-src[i]>=dst[ri]+dst[li]:
			x=(dst[i]-src[i])//(dst[ri]+dst[li])
			dst[i]-=(dst[ri]+dst[li])*x
			x+=SandQ(li)
			x+=SandQ(ri)
		return x;
	for i in range(n):
		ans+=SandQ(i)
	for i in range(n):
		if src[i]!=dst[i]:
			print((-1))
			exit()
	else:print(ans)
main()
# Original solution
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

from heapq import heappop, heappush

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

q = []
push = lambda x,i: heappush(q, (-x,i))
def pop():
    x,i = heappop(q)
    return -x,i

for i,x in enumerate(B):
    push(x,i)

answer = 0
while q:
    x,i = pop()
    a = A[i]
    dx = x-a
    if dx<0:
        answer = -1
        break
    if dx == 0:
        continue
    if i < N-1:
        nbd = B[i-1] + B[i+1]
    else:
        nbd = B[i-1] + B[0]
    t = dx // nbd
    if t == 0:
        answer = -1
        break
    B[i] -= nbd * t
    answer += t
    push(B[i],i)

print(answer)



# Optimized solution
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

from heapq import heappop, heappush

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

q = []
push = lambda x,i: heappush(q, (-x,i))
for i,x in enumerate(B):
    push(x,i)

answer = 0
while q:
    x,i = heappop(q)
    x = -x
    if x < A[i]:
        answer = -1
        break
    dx = x-A[i]
    nbd = B[(i-1)%N] + B[(i+1)%N]
    t = dx // nbd
    if t == 0:
        if x == A[i]:
            continue
        answer = -1
        break
    B[i] -= nbd * t
    answer += t
    push(B[i],i)

print(answer)
# Original solution
import sys
from heapq import heappush as hpu
from heapq import heappop as hpo
input = sys.stdin.readline
N = int(eval(input()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))

base = 10 ** 10
sb = [(-b[i], i) for i in range(N) if a[i] != b[i]]
h = []
for t in sb: hpu(h, t)
res = 0
while len(h):
  v, i = hpo(h)
  v = -v
  if v < a[i]:
    print((-1))
    exit(0)
  d = v - a[i]
  x = b[i - 1] + b[(i + 1) % N]
  t = d // x
  if t == 0:
    if v == a[i]: continue
    print((-1))
    exit(0)
  b[i] -= x * (t)
  res += t
  hpu(h, (-b[i], i))
print(res)


# Optimized solution
import sys
from heapq import heappush as hpu
from heapq import heappop as hpo
input = sys.stdin.readline
N = int(eval(input()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))

base = 10 ** 6
sb = [-(b[i] * base + i) for i in range(N) if a[i] != b[i]]
h = []
for t in sb: hpu(h, t)
res = 0
while len(h):
  v = hpo(h)
  i = -v % base
  v = -v // base
  if v < a[i]:
    print((-1))
    exit(0)
  d = v - a[i]
  x = b[i - 1] + b[(i + 1) % N]
  t = d // x
  if t == 0:
    if v == a[i]: continue
    print((-1))
    exit(0)
  b[i] -= x * (t)
  res += t
  hpu(h, -(b[i] * base + i))
print(res)