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
#include <bits/stdc++.h>
using namespace std;
const long long MX = 1e6 + 9, INF = 1e9;
vector<long long> adj[MX], roots[4];
long long mrk[MX], dis[4][MX];
void BFS(long long col) {
  fill(mrk, mrk + MX, 0);
  fill(dis[col], dis[col] + MX, INF);
  queue<long long> q;
  for (long long i = 0; i < roots[col].size(); i++)
    q.push(roots[col][i]), mrk[roots[col][i]] = 1, dis[col][roots[col][i]] = 0;
  while (!q.empty()) {
    long long u = q.front();
    q.pop();
    for (auto v : adj[u])
      if (!mrk[v]) {
        mrk[v] = 1;
        dis[col][v] = dis[col][u] + 1;
        q.push(v);
      }
  }
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  long long n, m;
  cin >> n >> m;
  string s[n];
  for (long long i = 0; i < n; i++) cin >> s[i];
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < m; j++) {
      long long u = i * m + j;
      if (s[i][j] != '#') {
        if (i > 0 && s[i - 1][j] != '#') adj[u].push_back(u - m);
        if (i < n - 1 && s[i + 1][j] != '#') adj[u].push_back(u + m);
        if (j > 0 && s[i][j - 1] != '#') adj[u].push_back(u - 1);
        if (j < m - 1 && s[i][j + 1] != '#') adj[u].push_back(u + 1);
      }
      if (s[i][j] == '1') roots[1].push_back(u);
      if (s[i][j] == '2') roots[2].push_back(u);
      if (s[i][j] == '3') roots[3].push_back(u);
    }
  }
  BFS(1);
  BFS(2);
  BFS(3);
  long long ans = INF;
  for (long long i = 0; i < n * m; i++)
    ans = min(ans, dis[1][i] + dis[2][i] + dis[3][i] - 2);
  for (long long k = 1; k < 4; k++) {
    long long min1 = INF, min2 = INF, min3 = INF;
    for (long long i = 0; i < roots[k].size(); i++) {
      min1 = min(min1, dis[1][roots[k][i]]);
      min2 = min(min2, dis[2][roots[k][i]]);
      min3 = min(min3, dis[3][roots[k][i]]);
    }
    ans = min(ans, min1 + min2 + min3 - 2);
  }
  if (ans > INF - 10) return cout << -1, 0;
  cout << ans;
  return 0;
}
