| 시간 제한 | 메모리 제한 | 제출 | 정답 | 맞힌 사람 | 정답 비율 |
|---|---|---|---|---|---|
| 2 초 | 1024 MB | 35 | 31 | 25 | 86.207% |
You got $HW$ cards, where $HW$ is even. Each card has a number 1ドル$ through $HW/2$ on the face, and exactly two cards have the same number. You considered what types of card games you could play with the cards, and decided to play 1-player concentration.
In the 1-player concentration, you first align $HW$ cards face down on the $H \times W$ rectangle field. Your goal is to remove all the cards from the field by repeating turns. For each turn, you must flip exactly two cards. If the two flipped cards have the same number on the face, you remove the two cards from the field. If not, you flip the two cards again to make them face down. But because you have perfect memory, you can remember which card has which number and where it's placed if you flipped the card once. So for each turn, you will act as follows.
Let's number the rows 1ドル$ through $H$ from the top. The card at the topmost row among the remaining cards has the highest precedence. If there are multiple cards at the topmost row, if the row is initially an odd-numbered row, the leftmost card has the highest precedence. If the row is initially an even-numbered row, the rightmost card has the highest precedence.
After you played the game, you noticed you forgot to count how many turns you took to remove all the cards from the field. Fortunately, you remember the initial placement of the cards. So you decided to write a program to compute the turns you take to remove all the cards for a given initial placement.
The first line contains two integers $H$ (1ドル ≤ H ≤ 100$) and $W$ (1ドル ≤ W ≤ 100$). You can assume $H \times W$ is even. Each of the following $H$ lines has exactly $W$ integers. The $j$-th integer of the $i$-th row represents the number on the face of a card at the $i$-th from the top and the $j$-th from the left. You can assume all the integers in the $H$ lines are no less than 1ドル,ドル no more than $HW/2,ドル and the same integer appears exactly twice.
Output in a line a single integer, which is the number of turns you take to remove all the cards.
2 6 5 5 1 4 4 1 6 2 3 2 3 6
9
4 3 1 1 2 3 3 2 4 4 5 6 6 5
6
1 10 1 2 3 4 5 5 4 3 2 1
7