atcoder#AGC037C. [AGC037C] Numbers on a Circle

[AGC037C] Numbers on a Circle

Score : 800800 points

Problem Statement

There are NN positive integers arranged in a circle.

Now, the ii-th number is AiA_i. Takahashi wants the ii-th number to be BiB_i. For this objective, he will repeatedly perform the following operation:

  • Choose an integer ii such that 1iN1 \leq i \leq N.
  • Let a,b,ca, b, c be the (i1)(i-1)-th, ii-th, and (i+1)(i+1)-th numbers, respectively. Replace the ii-th number with a+b+ca+b+c.

Here the 00-th number is the NN-th number, and the (N+1)(N+1)-th number is the 11-st number.

Determine if Takahashi can achieve his objective. If the answer is yes, find the minimum number of operations required.

Constraints

  • 3N2×1053 \leq N \leq 2 \times 10^5
  • 1Ai,Bi1091 \leq A_i, B_i \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN

A1A_1 A2A_2 ...... ANA_N

B1B_1 B2B_2 ...... BNB_N

Output

Print the minimum number of operations required, or -1 if the objective cannot be achieved.

3
1 1 1
13 5 7
4

Takahashi can achieve his objective by, for example, performing the following operations:

  • Replace the second number with 33.
  • Replace the second number with 55.
  • Replace the third number with 77.
  • Replace the first number with 1313.
4
1 2 3 4
2 3 4 5
-1
5
5 6 5 2 1
9817 1108 6890 4343 8704
25