#ARC120C. [ARC120C] Swaps 2

[ARC120C] Swaps 2

Score : 500500 points

Problem Statement

Given are two sequences of length NN each: A=(A1,A2,A3,,AN)A = (A_1, A_2, A_3, \dots, A_N) and B=(B1,B2,B3,,BN)B = (B_1, B_2, B_3, \dots, B_N). Determine whether it is possible to make AA equal BB by repeatedly doing the operation below (possibly zero times). If it is possible, find the minimum number of operations required to do so.

  • Choose an integer ii such that 1i<N1 \le i \lt N, and do the following in order:- swap AiA_i and Ai+1A_{i + 1};
    • add 11 to AiA_i;
    • subtract 11 from Ai+1A_{i + 1}.
  • swap AiA_i and Ai+1A_{i + 1};
  • add 11 to AiA_i;
  • subtract 11 from Ai+1A_{i + 1}.

Constraints

  • 2N2×1052 \le N \le 2 \times 10^5
  • 0Ai1090 \le A_i \le 10^9
  • 0Bi1090 \le B_i \le 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN

A1A_1 A2A_2 A3A_3 \dots ANA_N

B1B_1 B2B_2 B3B_3 \dots BNB_N

Output

If it is impossible to make AA equal BB, print -1. Otherwise, print the minimum number of operations required to do so.

3
3 1 4
6 2 0
2

We can match AA with BB in two operations, as follows:

  • First, do the operation with i=2i = 2, making A=(3,5,0)A = (3, 5, 0).
  • Next, do the operation with i=1i = 1, making A=(6,2,0)A = (6, 2, 0).

We cannot meet our objective in one or fewer operations.

3
1 1 1
1 1 2
-1

In this case, it is impossible to match AA with BB.

5
5 4 1 3 2
5 4 1 3 2
0

AA may equal BB before doing any operation.

6
8 5 4 7 4 5
10 5 6 7 4 1
7