#include <bits/stdc++.h>
using namespace std;
string s[1003];
int n, m;
int dist[3][1003][1003];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, -1, 0, 1};
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
  }
  memset(dist, -1, sizeof(dist));
  deque<pair<int, int> > q;
  for (char c = '1'; c < '4'; c++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (s[i][j] == c) {
          q.push_back(make_pair(i, j));
          dist[c - '1'][i][j] = 0;
        }
      }
    }
    while (!q.empty()) {
      int x = q.front().first, y = q.front().second;
      q.pop_front();
      for (int i = 0; i < 4; i++) {
        int nx = x + dx[i], ny = y + dy[i];
        if (nx >= 0 && nx < n && ny >= 0 && ny < m && s[nx][ny] != '#') {
          int nd = dist[c - '1'][x][y] + (s[nx][ny] == '.');
          if (dist[c - '1'][nx][ny] == -1 || dist[c - '1'][nx][ny] > nd) {
            dist[c - '1'][nx][ny] = nd;
            if (s[nx][ny] == '.')
              q.push_back(make_pair(nx, ny));
            else
              q.push_front(make_pair(nx, ny));
          }
        }
      }
    }
  }
  int ans = -1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (dist[0][i][j] != -1 && dist[1][i][j] != -1 && dist[2][i][j] != -1) {
        int val = dist[0][i][j] + dist[1][i][j] + dist[2][i][j] -
                  2 * (s[i][j] == '.');
        if (ans == -1 || ans > val) ans = val;
      }
    }
  }
  cout << ans << endl;
  return 0;
}

### Prompt

Your challenge is to write a CPP solution to the following problem:

The famous global economic crisis is approaching rapidly, so the states of Berman, Berance and Bertaly formed an alliance and allowed the residents of all member states to freely pass through the territory of any of them. In addition, it was decided that a road between the states should be built to guarantee so that one could any point of any country can be reached from any point of any other State.

Since roads are always expensive, the governments of the states of the newly formed alliance asked you to help them assess the costs. To do this, you have been issued a map that can be represented as a rectangle table consisting of n rows and m columns. Any cell of the map either belongs to one of three states, or is an area where it is allowed to build a road, or is an area where the construction of the road is not allowed. A cell is called passable, if it belongs to one of the states, or the road was built in this cell. From any passable cells you can move up, down, right and left, if the cell that corresponds to the movement exists and is passable.

Your task is to construct a road inside a minimum number of cells, so that it would be possible to get from any cell of any state to any cell of any other state using only passable cells.

It is guaranteed that initially it is possible to reach any cell of any state from any cell of this state, moving only along its cells. It is also guaranteed that for any state there is at least one cell that belongs to it.

Input

The first line of the input contains the dimensions of the map n and m (1 ≤ n, m ≤ 1000) — the number of rows and columns respectively.

Each of the next n lines contain m characters, describing the rows of the map. Digits from 1 to 3 represent the accessory to the corresponding state. The character '.' corresponds to the cell where it is allowed to build a road and the character '#' means no construction is allowed in this cell.

Output

Print a single integer — the minimum number of cells you need to build a road inside in order to connect all the cells of all states. If such a goal is unachievable, print -1.

Examples

Input

4 5
11..2
#..22
#.323
.#333

Output

2

Input

1 5
1#2#3


Output

-1

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
string s[1003];
int n, m;
int dist[3][1003][1003];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, -1, 0, 1};
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
  }
  memset(dist, -1, sizeof(dist));
  deque<pair<int, int> > q;
  for (char c = '1'; c < '4'; c++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (s[i][j] == c) {
          q.push_back(make_pair(i, j));
          dist[c - '1'][i][j] = 0;
        }
      }
    }
    while (!q.empty()) {
      int x = q.front().first, y = q.front().second;
      q.pop_front();
      for (int i = 0; i < 4; i++) {
        int nx = x + dx[i], ny = y + dy[i];
        if (nx >= 0 && nx < n && ny >= 0 && ny < m && s[nx][ny] != '#') {
          int nd = dist[c - '1'][x][y] + (s[nx][ny] == '.');
          if (dist[c - '1'][nx][ny] == -1 || dist[c - '1'][nx][ny] > nd) {
            dist[c - '1'][nx][ny] = nd;
            if (s[nx][ny] == '.')
              q.push_back(make_pair(nx, ny));
            else
              q.push_front(make_pair(nx, ny));
          }
        }
      }
    }
  }
  int ans = -1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (dist[0][i][j] != -1 && dist[1][i][j] != -1 && dist[2][i][j] != -1) {
        int val = dist[0][i][j] + dist[1][i][j] + dist[2][i][j] -
                  2 * (s[i][j] == '.');
        if (ans == -1 || ans > val) ans = val;
      }
    }
  }
  cout << ans << endl;
  return 0;
}

```