#include <bits/stdc++.h>
using namespace std;
const int G = 1010;
int n, m;
char chr;
int c[G][G];
long long dis[G][G][4];
queue<pair<int, int> > q;
void bfs(int k) {
  while (q.size()) {
    int x = q.front().first, y = q.front().second;
    q.pop();
    if ((x > 1) && (dis[x - 1][y][k] == -1) && (c[x - 1][y] != -1)) {
      dis[x - 1][y][k] = dis[x][y][k] + 1;
      q.push({x - 1, y});
    }
    if ((x < n) && (dis[x + 1][y][k] == -1) && (c[x + 1][y] != -1)) {
      dis[x + 1][y][k] = dis[x][y][k] + 1;
      q.push({x + 1, y});
    }
    if ((y > 1) && (dis[x][y - 1][k] == -1) && (c[x][y - 1] != -1)) {
      dis[x][y - 1][k] = dis[x][y][k] + 1;
      q.push({x, y - 1});
    }
    if ((y < m) && (dis[x][y + 1][k] == -1) && (c[x][y + 1] != -1)) {
      dis[x][y + 1][k] = dis[x][y][k] + 1;
      q.push({x, y + 1});
    }
  }
  return;
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> chr;
      if (chr == '#') c[i][j] = -1;
      if (chr == '1') c[i][j] = 1;
      if (chr == '2') c[i][j] = 2;
      if (chr == '3') c[i][j] = 3;
    }
  }
  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= m; j++)
      for (int k = 0; k < 4; k++) dis[i][j][k] = -1;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (c[i][j] == 1) q.push({i, j}), dis[i][j][1] = 0;
  bfs(1);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (c[i][j] == 2) q.push({i, j}), dis[i][j][2] = 0;
  bfs(2);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (c[i][j] == 3) q.push({i, j}), dis[i][j][3] = 0;
  bfs(3);
  long long cur, ans = LLONG_MAX;
  int cnt;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (c[i][j] != -1) {
        cnt = cur = 0;
        for (int k = 1; k < 4; k++)
          if (dis[i][j][k] != -1) cnt++, cur += dis[i][j][k];
        if (cnt == 3) ans = min(ans, cur - 2);
      }
  long long ans1 = LLONG_MAX, ans2 = LLONG_MAX;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      if (c[i][j] == 2 && dis[i][j][1] != -1) ans1 = min(ans1, dis[i][j][1]);
      if (c[i][j] == 3 && dis[i][j][1] != -1) ans2 = min(ans2, dis[i][j][1]);
    }
  if (ans1 != LLONG_MAX && ans2 != LLONG_MAX) ans = min(ans, ans1 + ans2 - 2);
  ans1 = ans2 = LLONG_MAX;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      if (c[i][j] == 1 && dis[i][j][2] != -1) ans1 = min(ans1, dis[i][j][2]);
      if (c[i][j] == 3 && dis[i][j][2] != -1) ans2 = min(ans2, dis[i][j][2]);
    }
  if (ans1 != LLONG_MAX && ans2 != LLONG_MAX) ans = min(ans, ans1 + ans2 - 2);
  ans1 = ans2 = LLONG_MAX;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      if (c[i][j] == 1 && dis[i][j][3] != -1) ans1 = min(ans1, dis[i][j][3]);
      if (c[i][j] == 2 && dis[i][j][3] != -1) ans2 = min(ans2, dis[i][j][3]);
    }
  if (ans1 != LLONG_MAX && ans2 != LLONG_MAX) ans = min(ans, ans1 + ans2 - 2);
  if (ans == LLONG_MAX)
    cout << -1;
  else
    cout << ans;
}
