The input contains no more than 20 test cases. For each test case, the first line contains a single integer N (2 \leq N \leq 8) indicating the size of game board.
The following N lines show an N×N matrixn×n representing the game board.
is in the range of 0 to 5 representing the color of the corresponding grid.
The input ends with N = 0.
For each test case, output a single integer representing the minimal number of steps to win the game.