Problem: There is a rectangular area containing n × m cells. Two cells are marked with "2", and another two with "3". Some cells are occupied by obstacles. You should connect the two "2"s and also the two "3"s with non-intersecting lines. Lines can run only vertically or horizontally connecting centers of cells without obstacles.

Lines cannot run on a cell with an obstacle. Only one line can run on a cell at most once. Hence, a line cannot intersect with the other line, nor with itself. Under these constraints, the total length of the two lines should be minimized. The length of a line is defined as the number of cell borders it passes. In particular, a line connecting cells sharing their border has length 1.

Fig. 6(a) shows an example setting. Fig. 6(b) shows two lines satisfying the constraints above with minimum total length 18.


<image>

Figure 6: An example setting and its solution



Input

The input consists of multiple datasets, each in the following format.


n m
row1
...
rown


n is the number of rows which satisfies 2 ≤ n ≤ 9. m is the number of columns which satisfies 2 ≤ m ≤ 9. Each rowi is a sequence of m digits separated by a space. The digits mean the following.

0: Empty

1: Occupied by an obstacle

2: Marked with "2"

3: Marked with "3"

The end of the input is indicated with a line containing two zeros separated by a space.

Output

For each dataset, one line containing the minimum total length of the two lines should be output. If there is no pair of lines satisfying the requirement, answer "0" instead. No other characters should be contained in the output.

Example

Input

5 5
0 0 0 0 0
0 0 0 3 0
2 0 2 0 0
1 0 1 1 1
0 0 0 0 3
2 3
2 2 0
0 3 3
6 5
2 0 0 0 0
0 3 0 0 0
0 0 0 0 0
1 1 1 0 0
0 0 0 0 0
0 0 2 3 0
5 9
0 0 0 0 0 0 0 0 0
0 0 0 0 3 0 0 0 0
0 2 0 0 0 0 0 2 0
0 0 0 0 3 0 0 0 0
0 0 0 0 0 0 0 0 0
9 9
3 0 0 0 0 0 0 0 2
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
2 0 0 0 0 0 0 0 3
9 9
0 0 0 1 0 0 0 0 0
0 2 0 1 0 0 0 0 3
0 0 0 1 0 0 0 0 2
0 0 0 1 0 0 0 0 3
0 0 0 1 1 1 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
9 9
0 0 0 0 0 0 0 0 0
0 3 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 2 3 2
0 0


Output

18
2
17
12
0
52
43

Solution (in CPP):

Solution:
#include <iostream>
#include <queue>
#include <cstring>

using namespace std;

int abs(int x){ return x>0?x:-x; }
int w, h, mdist, s2x, s2y, s3x, s3y,  ans;
int mp[9][9];
char dx[] = {1,0,-1,0};
char dy[] = {0,1,0,-1};

bool outBoard(int x, int y){
  return (x<0||h<=x||y<0||w<=y);
}
int bfs(int step){
  bool visit[9][9];
  memset(visit, false, sizeof(visit));
  queue< pair<int, pair<int,int> > > qu;
  qu.push(make_pair(0,make_pair(s3x,s3y)));
  while(!qu.empty()){
    pair<int, pair<int,int> > p = qu.front(); qu.pop();
    int s = p.first, x = p.second.first, y = p.second.second;
    if(s+step+1>=ans) return -1;
    if(visit[x][y]) continue;
    visit[x][y] = true;
    for(int i=0;i<4;i++){
      int nx = x+dx[i], ny = y+dy[i];
      if(outBoard(nx,ny)||visit[nx][ny]) continue;
      if(mp[nx][ny]==5) return s+1;
      if(mp[nx][ny]==0) qu.push(make_pair(s+1,make_pair(nx,ny)));
    }
  }
  return -1;
}

void dfs(int x, int y, int p0, int p1, int p2, int p3, int step){
  if(step+mdist>=ans) return;
  bool check = false;
  for(int i=0;i<4;i++){
    int nx = x+dx[i], ny = y+dy[i];
    if(outBoard(nx,ny)) continue;
    if(mp[nx][ny] == 4) check = true;
  }
  if(check){
    int cost = bfs(step);
    if(cost!=-1) ans = step+cost;
  } else {
    for(int i=0;i<4;i++){
      int nx = x+dx[i], ny = y+dy[i];
      if(outBoard(nx,ny)||mp[nx][ny]!=0) continue;
      int count = 0;
      for(int j=0;j<4;j++){
	int nnx = nx+dx[j], nny = ny+dy[j];
	if(outBoard(nnx,nny)) continue;
	if(mp[nnx][nny]==6||mp[nnx][nny]==2) count++;
      }
      if(count>1) continue;
      if(p1==p0&&(i-p2+4)%4==2&&(i+p1)%2==1){
	if(mp[nx-dx[p0]][ny-dy[p0]]==0) continue;
      }
      if(p2==p1&&p1==p0&&(i-p3+4)%4==2&&(i+p2)%2==1){
      	if(mp[nx-dx[p0]][ny-dy[p0]]==0&&mp[nx-2*dx[p0]][ny-2*dy[p0]]==0) continue;
      }
      mp[nx][ny] = 6;
      dfs(nx, ny, i, p0, p1, p2, step+1);
      mp[nx][ny] = 0;
    }
  }
}

int main(){
  while(cin >> h >> w, h){
    bool flag2 = true, flag3 = true;
    ans = 100;
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	cin >> mp[i][j];
	if(mp[i][j]==2){ 
	  if(flag2) mp[i][j] = 4, flag2 = false;
	  else s2x = i, s2y = j;
	}
	if(mp[i][j]==3){
	  if(flag3) s3x = i, s3y = j, mp[i][j] = 5, flag3 = false;
	  else mdist = abs(s3x-i)+abs(s3y-j), s3x = i, s3y = j;
	}
      }
    }
    dfs(s2x, s2y, 6, 7, 8, 9, 1);
    cout << (ans==100?0:ans) << endl;
  }
}