Browsing the Collection

아직 제출이 없습니다시간 제한4초메모리 제한512 MB

문제

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 i1i - 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 a_i,ja\_{i, j}.

While you are browsing, at any moment, there is a pointer directed at some item, called the current item.

Moreover, you can 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 through the collection, you can do operations. Each operation must have one of the following four kinds:

  • Click "right". The pointer moves to the closest item to the right of the current item satisfying all filtering conditions. In particular, if the current item is the only such item, the pointer does not move.
  • Click "left". Similarly, the pointer moves to the closest item to the left of the current item satisfying all filtering conditions, and stays at the same place if the current item is the only such item.
  • Add a new filtering condition (j,v)(j, v), for some integers jj and vv. 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 satisfying all filtering conditions, including the new one. If there is no such item, the operation is illegal and can not 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 the collection with the pointer directed at item ii and with no filtering conditions in the set, what is the smallest number of operations you need to move the pointer to item jj? The set of filtering conditions may be arbitrary at the end.

입력

The first line contains two integers nn and mm, denoting the number of items in the collection and the number of parameters each item has (2n5002 \le n \le 500; 1m51 \le m \le 5).

The ii-th of the next nn lines contains mm integers a_i,1,a_i,2,,a_i,ma\_{i, 1}, a\_{i, 2}, \ldots, a\_{i, m}, denoting the parameter values of item ii (1a_i,jn1 \le a\_{i, j} \le n).

출력

Print nn lines containing nn integers each.

In the ii-th line, the jj-th integer must be equal to the smallest number of operations required to move the pointer from item ii to item jj, starting with an empty set of filtering conditions.

힌트

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

  • Add a new filtering condition (3,4)(3, 4). Since item 22 indeed has the value of parameter 33 equal to 44, the pointer stays at item 22.
  • Click "right". The pointer moves to the closest item to the right of item 22 satisfying the only active condition (3,4)(3, 4). This item is item 55. (Clicking "left" instead works as well.)

Here is one possible fastest way to move from item 88 to item 33:

  • Add a new filtering condition (3,4)(3, 4). Since item 88 does not satisfy this condition, the pointer moves to the closest item to the right of item 88 with the value of 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". Since there are no filtering conditions in the set, the pointer moves to item 33.