#include <bits/stdc++.h>
using namespace std;
const int MOD = (int)1e9 + 7;
const int MAXN = (int)3007;
const int base = 37;
const int infint = (int)1e9;
const long long inf = (long long)1e18 + 9;
char c[MAXN][MAXN];
bool c12 = false, c23 = false, c13 = false;
int dist1[MAXN][MAXN], dist2[MAXN][MAXN], dist3[MAXN][MAXN];
int n, m;
queue<pair<int, int> > Q;
void clear(queue<pair<int, int> > &Q) {
  queue<pair<int, int> > empty;
  swap(Q, empty);
}
void bfs1() {
  while (!Q.empty()) {
    pair<int, int> v = Q.front();
    Q.pop();
    int i = v.first, j = v.second;
    if (i != 1 and c[i - 1][j] != '#' and dist1[i - 1][j] == infint) {
      dist1[i - 1][j] = dist1[i][j] + 1;
      Q.push({i - 1, j});
    }
    if (i != n and c[i + 1][j] != '#' and dist1[i + 1][j] == infint) {
      dist1[i + 1][j] = dist1[i][j] + 1;
      Q.push({i + 1, j});
    }
    if (j != 1 and c[i][j - 1] != '#' and dist1[i][j - 1] == infint) {
      dist1[i][j - 1] = dist1[i][j] + 1;
      Q.push({i, j - 1});
    }
    if (j != m and c[i][j + 1] != '#' and dist1[i][j + 1] == infint) {
      dist1[i][j + 1] = dist1[i][j] + 1;
      Q.push({i, j + 1});
    }
  }
}
void bfs2() {
  while (!Q.empty()) {
    pair<int, int> v = Q.front();
    Q.pop();
    int i = v.first, j = v.second;
    if (i != 1 and c[i - 1][j] != '#' and dist2[i - 1][j] == infint) {
      dist2[i - 1][j] = dist2[i][j] + 1;
      Q.push({i - 1, j});
    }
    if (i != n and c[i + 1][j] != '#' and dist2[i + 1][j] == infint) {
      dist2[i + 1][j] = dist2[i][j] + 1;
      Q.push({i + 1, j});
    }
    if (j != 1 and c[i][j - 1] != '#' and dist2[i][j - 1] == infint) {
      dist2[i][j - 1] = dist2[i][j] + 1;
      Q.push({i, j - 1});
    }
    if (j != m and c[i][j + 1] != '#' and dist2[i][j + 1] == infint) {
      dist2[i][j + 1] = dist2[i][j] + 1;
      Q.push({i, j + 1});
    }
  }
}
void bfs3() {
  while (!Q.empty()) {
    pair<int, int> v = Q.front();
    Q.pop();
    int i = v.first, j = v.second;
    if (i != 1 and c[i - 1][j] != '#' and dist3[i - 1][j] == infint) {
      dist3[i - 1][j] = dist3[i][j] + 1;
      Q.push({i - 1, j});
    }
    if (i != n and c[i + 1][j] != '#' and dist3[i + 1][j] == infint) {
      dist3[i + 1][j] = dist3[i][j] + 1;
      Q.push({i + 1, j});
    }
    if (j != 1 and c[i][j - 1] != '#' and dist3[i][j - 1] == infint) {
      dist3[i][j - 1] = dist3[i][j] + 1;
      Q.push({i, j - 1});
    }
    if (j != m and c[i][j + 1] != '#' and dist3[i][j + 1] == infint) {
      dist3[i][j + 1] = dist3[i][j] + 1;
      Q.push({i, j + 1});
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      cin >> c[i][j];
      dist1[i][j] = infint, dist2[i][j] = infint, dist3[i][j] = infint;
    }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (c[i][j] == '1') {
        dist1[i][j] = 0;
        Q.push({i, j});
      }
  bfs1();
  clear(Q);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (c[i][j] == '2') {
        dist2[i][j] = 0;
        Q.push({i, j});
      }
  bfs2();
  clear(Q);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (c[i][j] == '3') {
        dist3[i][j] = 0;
        Q.push({i, j});
      }
  bfs3();
  int mn = infint;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (dist1[i][j] != infint and dist2[i][j] != infint and
          dist3[i][j] != infint) {
        mn = min(mn, dist1[i][j] + dist2[i][j] + dist3[i][j] - 2);
      }
  int mn2 = infint, mn3 = infint;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (c[i][j] == '1') {
        mn2 = min(mn2, dist2[i][j]);
        mn3 = min(mn3, dist3[i][j]);
      }
  if (mn2 != infint and mn3 != infint) mn = min(mn, mn2 + mn3 - 2);
  mn2 = infint, mn3 = infint;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (c[i][j] == '2') {
        mn2 = min(mn2, dist1[i][j]);
        mn3 = min(mn3, dist3[i][j]);
      }
  if (mn2 != infint and mn3 != infint) mn = min(mn, mn2 + mn3 - 2);
  mn2 = infint, mn3 = infint;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (c[i][j] == '3') {
        mn2 = min(mn2, dist1[i][j]);
        mn3 = min(mn3, dist2[i][j]);
      }
  if (mn2 != infint and mn3 != infint) mn = min(mn, mn2 + mn3 - 2);
  if (mn == infint) mn = -1;
  cout << mn;
}
