A Bingo game is played by one gamemaster and several players. At the beginning of a game, each player is given a card with M *M numbers in a matrix (See Figure 10).
As the game proceeds, the gamemaster announces a series of numbers one by one. Each player punches a hole in his card on the announced number, if any.
When at least one 'Bingo' is made on the card, the player wins and leaves the game. The 'Bingo' means that all the M numbers in a line are punched vertically, horizontally or diagonally (See Figure 11).
The gamemaster continues announcing numbers until all the players make a Bingo.
In the ordinary Bingo games, the gamemaster chooses numbers by a random process and has no control on them. But in this problem the gamemaster knows all the cards at the beginning of the game and controls the game by choosing the number sequence to be announced at his will.
Specifically, he controls the game to satisfy the following condition.
Cardi makes a Bingo no later than Cardj, for i < j. (*)
Figure 12 shows an example of how a game proceeds. The gamemaster cannot announce '5' before '16', because Card4 makes a Bingo before Card2 and Card3, violating the condition (*).
Your job is to write a program which finds the minimum length of such sequence of numbers for the given cards.