Under construction

Still being built. Things may change

Rotation Parity

Time limit5sMemory limit256 MB

Summary
Decide the parity of the number of 2x2 clockwise rotations needed to sort a permutation of an R x C grid into row-major order.
Level

Hard8 of 10

Topics
Math, Combinatorics, Implementation, Sorting
Solved
No attempts yet

Problem

The archaeologist Diana Jones has found a secret passage to hidden treasure near Nowhere, Kansas. A stone gate blocks the passage, and an ancient unlocking mechanism is chiselled into the gate. Diana recognized the chiselled symbols at once.

  1. The mechanism is a table with RR rows and CC columns. Each cell holds one integer between 11 and R×CR \times C, and no value appears twice. At first glance the numbers look randomly placed.
  2. The mechanism has cogwheels that rearrange the cells. In one move Diana rotates a 2×22 \times 2 group of adjacent cells clockwise by 9090 degrees.
  3. The gate opens when the numbers are sorted in row-major order. The upper left cell must hold 11, the cell to its right 22, and so on until the lower right cell, which must hold R×CR \times C.

The arrangement in the picture below is unlocked by two moves.

Diana does not need the moves themselves, only whether their number is even or odd. One arrangement admits many unlocking sequences, and all of them have the same length parity. Given the initial arrangement, write a program that decides whether the number of moves is even or odd.

Input

The first line contains the integers RR and CC (2≤R≤C≤252 \le R \le C \le 25).

Each of the next RR lines contains CC integers ZijZ_{ij} (1≤Zij≤R×C1 \le Z_{ij} \le R \times C), the values chiselled into the cells of the initial arrangement. All values are different.

The given arrangement can always be unlocked.

Output

Print 00 if the number of moves is even and 11 if it is odd.

Hint

The arrangement in the picture is sorted by first rotating the group whose upper left corner sits in row 1 and column 1, then the group whose upper left corner sits in row 1 and column 2. That is two moves, so the answer is 00.

Examples3

  1. Example 1

    Input
    2 3
    3 2 6
    1 4 5
    
    Expected output
    0
    
  2. Example 2

    Input
    3 3
    1 2 3
    4 6 9
    7 5 8
    
    Expected output
    1
    
  3. Example 3

    Input
    2 4
    1 2 7 3
    5 6 8 4
    
    Expected output
    1