The path in the colored field 

The Problem

The square field consists of M×M cells. Each cell is colored in one of three colors (1,2,3). The initial state is chosen in one of the cells of color 1. In each step one allowed to move one cell up, down, left or right remaining inside the field.

You are to define the minimal amount of steps one should make to get a cell of color 3 independent on the initial state.

Note that the field contains at least one cell of color 1 and at least one cell of color 3.

The Input

The input consists of several input blocks. The first line of each block contains integer M, the size of the field. Then there are M lines with colors of the cells.

The Output

For each input block the output should consist of one line with the integer, the minimal amount of steps one should make to get a cell of color 3 independent on the initial state.

Sample Input

4
1223
2123
2213
3212
2
12
33

Sample Output

3
1