#include <bits/stdc++.h>
using namespace std;
const int maxn = 1100;
const int INF = (1 << 29);
const double EPS = 0.0000001;
const double Pi = acos(-1.0);
int n, m;
char ch[maxn][maxn];
int d[3][maxn][maxn];
bool vis[maxn][maxn];
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
struct Node {
  int x, y;
  int step;
};
bool f12, f23, f13, f123;
void bfs(char c) {
  memset(d[c - '1'], -1, sizeof(d[c - '1']));
  memset(vis, 0, sizeof(vis));
  queue<Node> q;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (ch[i][j] == c) q.push({i, j, 0}), vis[i][j] = 1;
    }
  }
  while (!q.empty()) {
    Node u = q.front();
    q.pop();
    d[c - '1'][u.x][u.y] = u.step;
    for (int i = 0; i <= 3; i++) {
      int nx = u.x + dx[i], ny = u.y + dy[i];
      if (ch[nx][ny] == '#' || vis[nx][ny]) continue;
      vis[nx][ny] = 1;
      q.push({nx, ny, u.step + 1});
    }
  }
}
void debug(char c) {
  cout << endl;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      printf("%2d", d[c][i][j]);
    }
    cout << endl;
  }
}
void judge() {
  for (int x = 1; x <= n; x++) {
    for (int y = 1; y <= m; y++) {
      for (int i = 0; i <= 4; i++) {
        int nx = x + dx[i], ny = y + dy[i];
        if (ch[x][y] == '1' && ch[nx][ny] == '2') f12 = 1;
        if (ch[x][y] == '2' && ch[nx][ny] == '3') f23 = 1;
        if (ch[x][y] == '1' && ch[nx][ny] == '3') f13 = 1;
      }
    }
  }
  if (f12 && f23 || f23 && f13 || f13 && f12) f123 = 1;
}
void deal() {
  int r1 = INF, r2 = INF;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (ch[i][j] == '1') {
        r1 = min(d[1][i][j], r1);
        r2 = min(d[2][i][j], r2);
      }
    }
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (ch[i][j] == '1') d[1][i][j] = r1, d[2][i][j] = r2;
  r1 = INF, r2 = INF;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (ch[i][j] == '2') {
        r1 = min(d[0][i][j], r1);
        r2 = min(d[2][i][j], r2);
      }
    }
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (ch[i][j] == '2') d[0][i][j] = r1, d[2][i][j] = r2;
  r1 = INF, r2 = INF;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (ch[i][j] == '3') {
        r1 = min(d[0][i][j], r1);
        r2 = min(d[1][i][j], r2);
      }
    }
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (ch[i][j] == '3') d[0][i][j] = r1, d[1][i][j] = r2;
}
int main() {
  while (cin >> n >> m) {
    f12 = f23 = f13 = f123 = 0;
    memset(ch, '#', sizeof(ch));
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) cin >> ch[i][j];
    }
    judge();
    if (f123) {
      puts("0");
      continue;
    }
    bfs('1');
    bfs('2');
    bfs('3');
    deal();
    int ans = INF;
    if (f12) {
      for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
          bool tag = 1;
          for (int k = 0; k <= 2; k++) {
            if (d[k][i][j] == -1) tag = 0;
          }
          if (!tag) continue;
          int tmp = 0;
          tmp = min(d[0][i][j], d[1][i][j]) - 1 + d[2][i][j] - 1 + 1;
          ans = min(ans, tmp);
        }
      }
    } else if (f23) {
      for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
          bool tag = 1;
          for (int k = 0; k <= 2; k++) {
            if (d[k][i][j] == -1) tag = 0;
          }
          if (!tag) continue;
          int tmp = 0;
          tmp = min(d[1][i][j], d[2][i][j]) - 1 + d[0][i][j] - 1 + 1;
          ans = min(ans, tmp);
        }
      }
    } else if (f13) {
      for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
          bool tag = 1;
          for (int k = 0; k <= 2; k++) {
            if (d[k][i][j] == -1) tag = 0;
          }
          if (!tag) continue;
          int tmp = 0;
          tmp = min(d[0][i][j], d[2][i][j]) - 1 + d[1][i][j] - 1 + 1;
          ans = min(ans, tmp);
        }
      }
    } else {
      for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
          bool tag = 1;
          for (int k = 0; k <= 2; k++) {
            if (d[k][i][j] == -1) tag = 0;
          }
          if (!tag) continue;
          int tmp = 0;
          tmp = d[0][i][j] + d[1][i][j] + d[2][i][j] - 3 + 1;
          ans = min(ans, tmp);
        }
      }
    }
    if (ans == INF)
      puts("-1");
    else
      cout << ans << endl;
  }
  return 0;
}
