#include <bits/stdc++.h>
using namespace std;
int dist1(vector<vector<int>> &v, int fx, int fy, int d) {
  if (d != 0)
    return 0;
  else
    return 1;
}
void print(vector<vector<int>> &v) {
  for (int i = 0; i < v.size(); i++) {
    for (int j = 0; j < v[i].size(); j++) {
      cout << v[i][j] << " ";
    }
    cout << endl;
  }
  cout << endl;
}
vector<vector<int>> dijkstra(int n, int m, vector<vector<int>> &v, int cx,
                             int cy, int city) {
  vector<bool> vb(m + 2);
  vector<vector<bool>> vis(n + 2, vb);
  set<pair<int, pair<int, int>>> queue;
  queue.insert(make_pair(0, make_pair(cx, cy)));
  vector<int> vdist(m + 2, numeric_limits<int>::max());
  vector<vector<int>> dist(n + 2, vdist);
  dist[cx][cy] = 0;
  while (!queue.empty()) {
    int d = queue.begin()->first;
    int x = queue.begin()->second.first;
    int y = queue.begin()->second.second;
    queue.erase(queue.begin());
    vis[x][y] = true;
    if (v[x - 1][y] != -1 && !vis[x - 1][y]) {
      if (d + dist1(v, x, y, v[x - 1][y]) < dist[x - 1][y]) {
        dist[x - 1][y] = d + dist1(v, x, y, v[x - 1][y]);
        queue.insert(make_pair(dist[x - 1][y], make_pair(x - 1, y)));
      }
    }
    if (v[x + 1][y] != -1 && !vis[x + 1][y]) {
      if (d + dist1(v, x, y, v[x + 1][y]) < dist[x + 1][y]) {
        dist[x + 1][y] = d + dist1(v, x, y, v[x + 1][y]);
        queue.insert(make_pair(dist[x + 1][y], make_pair(x + 1, y)));
      }
    }
    if (v[x][y - 1] != -1 && !vis[x][y - 1]) {
      if (d + dist1(v, x, y, v[x][y - 1]) < dist[x][y - 1]) {
        dist[x][y - 1] = d + dist1(v, x, y, v[x][y - 1]);
        queue.insert(make_pair(dist[x][y - 1], make_pair(x, y - 1)));
      }
    }
    if (v[x][y + 1] != -1 && !vis[x][y + 1]) {
      if (d + dist1(v, x, y, v[x][y + 1]) < dist[x][y + 1]) {
        dist[x][y + 1] = d + dist1(v, x, y, v[x][y + 1]);
        queue.insert(make_pair(dist[x][y + 1], make_pair(x, y + 1)));
      }
    }
  }
  return dist;
}
int main() {
  int n, m;
  string s;
  cin >> n >> m;
  vector<int> vv(m + 2);
  vector<vector<int>> v(n + 2, vv);
  for (int i = 0; i < n + 2; ++i) {
    v[i][0] = -1;
    v[i][m + 1] = -1;
  }
  for (int i = 0; i < m + 2; ++i) {
    v[0][i] = -1;
    v[n + 1][i] = -1;
  }
  vector<int> cx(4), cy(4);
  vector<bool> is(4);
  for (int i = 0; i < n; ++i) {
    cin >> s;
    for (int j = 0; j < m; ++j) {
      if (s[j] == '#') {
        v[i + 1][j + 1] = -1;
      } else if (s[j] == '.') {
        v[i + 1][j + 1] = 0;
      } else {
        char city = s[j] - '0';
        if (!is[city]) {
          is[city] = true;
          cx[city] = i + 1;
          cy[city] = j + 1;
        }
        v[i + 1][j + 1] = city;
      }
    }
  }
  vector<vector<int>> a = dijkstra(n, m, v, cx[1], cy[1], 2);
  vector<vector<int>> b = dijkstra(n, m, v, cx[2], cy[2], 3);
  vector<vector<int>> c = dijkstra(n, m, v, cx[3], cy[3], 1);
  int mm = numeric_limits<int>::max();
  int ans = numeric_limits<int>::max();
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (a[i][j] != mm && b[i][j] != mm && c[i][j] != mm) {
        ans = min(ans, a[i][j] + b[i][j] + c[i][j] - (v[i][j] == 0 ? 2 : 0));
      }
    }
  }
  if (ans == numeric_limits<int>::max()) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
}
