[Solution] Puzzle solution codeforces

Puzzle solution codeforces – Pupils Alice and Ibragim are best friends. It’s Ibragim’s birthday soon, so Alice decided to gift him a new puzzle. The puzzle can be represented as a matrix with 22 rows and 𝑛n columns, every element of which is either 00 or 11. In one move you can swap two values in neighboring cells.

Table of Contents

[Solution] Puzzle solution codeforces

More formally, let’s number rows 11 to 22 from top to bottom, and columns 11 to 𝑛n from left to right. Also, let’s denote a cell in row 𝑥x and column 𝑦y as (𝑥,𝑦)(x,y). We consider cells (𝑥1,𝑦1)(x1,y1) and (𝑥2,𝑦2)(x2,y2) neighboring if |𝑥1𝑥2|+|𝑦1𝑦2|=1|x1−x2|+|y1−y2|=1.

Alice doesn’t like the way in which the cells are currently arranged, so she came up with her own arrangement, with which she wants to gift the puzzle to Ibragim. Since you are her smartest friend, she asked you to help her find the minimal possible number of operations in which she can get the desired arrangement. Find this number, or determine that it’s not possible to get the new arrangement.

[Solution] Puzzle solution codeforces

The first line contains an integer 𝑛n (1𝑛2000001≤n≤200000) — the number of columns in the puzzle.

Following two lines describe the current arrangement on the puzzle. Each line contains 𝑛n integers, every one of which is either 00 or 11.

The last two lines describe Alice’s desired arrangement in the same format.

Output

If it is possible to get the desired arrangement, print the minimal possible number of steps, otherwise print 1−1.

Examples

input

Copy
5
0 1 0 1 0
1 1 0 0 1
1 0 1 0 1
0 0 1 1 0

[Solution] Puzzle solution codeforces

output

Copy
5

input

Copy
3
1 0 0
0 0 0
0 0 0
0 0 0

output

Copy
-1

[Solution] Puzzle solution codeforces

In the first example the following sequence of swaps will suffice:

  • (2,1),(1,1)(2,1),(1,1),
  • (1,2),(1,3)(1,2),(1,3),
  • (2,2),(2,3)(2,2),(2,3),
  • (1,4),(1,5)(1,4),(1,5),
  • (2,5),(2,4)(2,5),(2,4).

It can be shown that 55 is the minimal possible answer in this case.

In the second example no matter what swaps you do, you won’t get the desired arrangement, so the answer is 1−1.

For Solution

Click Here

Leave a Comment