This page is still under construction.

Parts of this page are still being built. What you see may change.

Browsing the Collection

Time limit4sMemory limit512 MB

Summary
For every pair of items on a circle, find the fewest clicks and filter changes needed to move the pointer from one to the other.
Level

Hard8 of 10

Topics
Shortest path, Graph, BFS
Solved
No attempts yet

Problem

You are browsing an online collection of nn items numbered from 11 to nn arranged on a circle. The item to the right of each item ii is item i+1i + 1, and the item to the right of item nn is item 11. Similarly, the item to the left of each item ii is item i−1i - 1, and the item to the left of item 11 is item nn.

The items have mm parameters numbered from 11 to mm. The value of parameter jj for item ii is an integer ai,ja_{i, j}.

While you are browsing, the pointer is always directed at some item, called the current item. You can also manage a set of filtering conditions. Each condition is a pair (j,v)(j, v), meaning that the jj-th parameter of the item must be equal to vv. The current item always satisfies all conditions in the set.

To browse the collection, you perform operations. Each operation must be one of the following four kinds:

  • Click right. The pointer moves to the closest item to the right of the current item that satisfies all filtering conditions. If the current item is the only such item, the pointer does not move.
  • Click left. The pointer moves to the closest item to the left of the current item that satisfies all filtering conditions. If the current item is the only such item, the pointer does not move.
  • Add a new filtering condition (j,v)(j, v). If the current item satisfies this condition, the pointer does not move. Otherwise, the pointer moves to the closest item to the right of the current item that satisfies all filtering conditions, including the new one. If there is no such item, the operation is illegal and cannot be performed.
  • Remove any filtering condition (j,v)(j, v) from the set. The pointer does not move.

For each ordered pair of items (i,j)(i, j), answer the following question: if you start browsing with the pointer at item ii and with no filtering conditions in the set, what is the smallest number of operations needed to move the pointer to item jj? The set of filtering conditions may be arbitrary at the end.

Input

The first line contains two integers nn and mm, the number of items and the number of parameters per item (2≤n≤5002 \le n \le 500; 1≤m≤51 \le m \le 5).

The ii-th of the next nn lines contains mm integers ai,1,ai,2,…,ai,ma_{i, 1}, a_{i, 2}, \ldots, a_{i, m}, the parameter values of item ii (1≤ai,j≤n1 \le a_{i, j} \le n).

Output

Print nn lines with nn integers each. In the ii-th line, the jj-th integer is the smallest number of operations required to move the pointer from item ii to item jj, starting with an empty set of filtering conditions.

Hint

In the example test, one fastest way to move from item 22 to item 55 is:

  • Add the filtering condition (3,4)(3, 4). Item 22 has parameter 33 equal to 44, so the pointer stays at item 22.
  • Click right. The pointer moves to the closest item to the right of item 22 that satisfies the only active condition (3,4)(3, 4). This item is item 55. (Clicking left works as well.)

One fastest way to move from item 88 to item 33 is:

  • Add the filtering condition (3,4)(3, 4). Item 88 does not satisfy it, so the pointer moves to the closest item to the right of item 88 with parameter 33 equal to 44. This item is item 22.
  • Remove the filtering condition (3,4)(3, 4). The pointer stays at item 22.
  • Click right. With no filtering conditions, the pointer moves to item 33.

Examples1

  1. Example 1

    Input
    9 3
    5 3 7
    5 3 4
    5 3 7
    5 3 2
    5 3 4
    5 3 7
    2 3 7
    5 3 7
    2 3 7
    
    Expected output
    0 1 2 1 2 3 1 2 1
    1 0 1 1 2 2 1 3 2
    2 1 0 1 1 2 1 3 2
    3 2 1 0 1 1 1 3 2
    3 2 2 1 0 1 1 3 2
    3 1 2 1 1 0 1 2 2
    2 1 3 1 2 1 0 1 2
    2 1 3 1 2 2 1 0 1
    1 1 3 1 2 3 2 1 0