luogu#P7796. [COCI 2014/2015 #7] POLICE

[COCI 2014/2015 #7] POLICE

Problem Description

In librarian Jurica's library, there are nn bookshelves, and each bookshelf has mm positions, numbered from 11 to mm from left to right. Each position can hold one book. Jurica is a good librarian, so he decides to take inventory of the library and, if necessary, return books that are not in their correct positions to where they originally belonged. Specifically, the book currently placed at position jj on bookshelf ii is numbered ai,ja_{i,j} (if ai,j=0a_{i,j}=0, it means the position is currently empty), while the book that originally belonged to this position is numbered bi,jb_{i,j} (if bi,j=0b_{i,j}=0, it means the position was originally empty). He moves books using the following operations:

  • Operation 11: If there is an empty position to the left or right, a book can be moved left or right along the same bookshelf.
  • Operation 22: Take a book off a bookshelf and place it into an empty position on the same bookshelf or on another bookshelf.

Careful Jurica cannot move books while holding a book in his hands. Also, he cannot hold more than one book at the same time.

Ever since he had to move all volumes of the printed edition of Wikipedia from the first floor to the second floor, Jurica has had back pain. Because his back hurts, he now wants to put all books into their correct places with as little carrying as possible. Please tell him the minimum number of times he needs to perform operation 22, or tell him that it is impossible to restore all books to their original positions.

Input Format

The input consists of 2n+12n+1 lines.

The first line contains two integers n,mn,m, representing the number of bookshelves and the number of positions on each bookshelf.
Lines 22 to n+1n+1 each contain mm integers ai,ja_{i,j}, describing the current arrangement of books on the shelves.
Lines n+2n+2 to 2n+12n+1 each contain mm integers bi,jb_{i,j}, describing the original arrangement of books on the shelves.

The input guarantees that the sets of books appearing in the original and current states are exactly the same.

Output Format

If Jurica cannot restore all books to their original positions, output a single integer 1-1.

Otherwise, output a single integer on one line, representing the minimum number of times operation 22 needs to be performed.

2 4
1 0 2 0
3 5 4 0
2 1 0 0
3 0 4 5
2
3 3
1 2 3
4 5 6
7 8 0
4 2 3
6 5 1
0 7 8
4
2 2
1 2
3 4
2 3
4 1
-1

Hint

[Sample 1 Explanation]

Below is Jurica's sequence of operations:

  1. Operation 11: Move book 11 to the position on its right, i.e. the second position on its bookshelf.
  2. Operation 22: Carry book 22 to the first position on its bookshelf.
  3. Operation 22: Carry book 55 to the second position on its bookshelf.

It can be proven that there is no solution with fewer executions of operation 22.

[Constraints]

For 50%50\% of the testdata, each book is guaranteed to stay on the same bookshelf in both the initial and final states.
For all testdata, 1n,m10001\leqslant n,m\leqslant 1000.

[Source]

This problem is from COCI 2014-2015 CONTEST 7 T6 POLICE, using the original testdata settings, with a full score of 160160 points.

Translated and整理 (pinyin: zhengli) by Eason_AC.

Translated by ChatGPT 5