A Bingo game is played by one gamemaster and several players. At the start of a game, each player receives a card with $M \times M$ numbers arranged in a matrix (see Figure 1).

Figure 1: A card

Figure 2: Bingo patterns of a 4x4 card
As the game proceeds, the gamemaster announces numbers one at a time. Whenever an announced number appears on a player's card, that player punches a hole over it.
As soon as at least one "Bingo" is completed on a card, that player wins and leaves the game. A "Bingo" is a line in which all $M$ numbers are punched, where a line is a full row, a full column, or one of the two main diagonals (see Figure 2).

Figure 3: Example of a Bingo game in progress
The gamemaster keeps announcing numbers until every player has made a Bingo.
In an ordinary Bingo game the gamemaster draws numbers at random and cannot control them. In this problem, however, the gamemaster knows every card from the beginning and controls the game by freely choosing the order in which numbers are announced.
The gamemaster steers the game so that the following condition always holds:
Card $i$ completes its Bingo no later than Card $j$, for every pair with $i < j$. $(*)$
For example, in the situation of Figure 3 the gamemaster must not announce $5$ before $16$: doing so would let Card 4 reach a Bingo before Card 2 and Card 3, violating condition $(*)$.
Write a program that computes the minimum possible length of such a sequence of announced numbers for the given cards.
The input consists of several datasets. Each dataset has the following form:
P M
N(1,1,1) N(1,1,2) ... N(1,1,M) N(1,2,1) ... N(1,M,M)
N(2,1,1) N(2,1,2) ... N(2,M,M)
...
N(P,1,1) N(P,1,2) ... N(P,M,M)
All values are integers. $P$ is the number of cards, which equals the number of players, and $M$ is both the number of rows and the number of columns of each card. $N_{kij}$ is the number written at position $(i, j)$ of the $k$-th card, and the $M \times M$ numbers of one card are listed row by row on a single line. All numbers on the same card are distinct: if $(i, j) \ne (p, q)$ then $N_{kij} \ne N_{kpq}$. The values satisfy $2 \le P \le 4$, $3 \le M \le 4$, and $0 \le N_{kij} \le 99$.
The end of the input is a line containing two zeros separated by a space; this line is not a dataset.
For each dataset, print on its own line the minimum length of a sequence of announced numbers that satisfies condition $(*)$. If no such sequence exists, print $0$.