#P1700F. Puzzle
Puzzle
Description
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 rows and columns, every element of which is either or . In one move you can swap two values in neighboring cells.
More formally, let's number rows to from top to bottom, and columns to from left to right. Also, let's denote a cell in row and column as . We consider cells and neighboring if .
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.
The first line contains an integer () — the number of columns in the puzzle.
Following two lines describe the current arrangement on the puzzle. Each line contains integers, every one of which is either or .
The last two lines describe Alice's desired arrangement in the same format.
If it is possible to get the desired arrangement, print the minimal possible number of steps, otherwise print .
Input
The first line contains an integer () — the number of columns in the puzzle.
Following two lines describe the current arrangement on the puzzle. Each line contains integers, every one of which is either or .
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 .
Samples
Note
In the first example the following sequence of swaps will suffice:
- ,
- ,
- ,
- ,
- .
It can be shown that 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 .