#include <bits/stdc++.h>
using namespace std;
void boost() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}
const int N = 1003;
string arr[N];
int n, m, ans[3], anst[3][N][N];
int findmin(int s, int t) {
  int a[n][m];
  queue<pair<int, int> > ss;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j) {
      if (arr[i][j] - '0' == s) {
        ss.push({0, m * i + j});
        a[i][j] = s;
      } else
        a[i][j] = 0;
    }
  int i, j, d;
  pair<int, int> top;
  while (!ss.empty()) {
    top = ss.front();
    ss.pop();
    i = top.second / m;
    j = top.second % m;
    d = top.first;
    if (j + 1 < m) {
      if (arr[i][j + 1] == '.') {
        if (a[i][j + 1] == 0) {
          a[i][j + 1] = s;
          ss.push({d + 1, m * i + j + 1});
        }
      } else if (arr[i][j + 1] - '0' == t) {
        return d;
      }
    }
    if (j - 1 >= 0) {
      if (arr[i][j - 1] == '.') {
        if (a[i][j - 1] == 0) {
          a[i][j - 1] = s;
          ss.push({d + 1, m * i + j - 1});
        }
      } else if (arr[i][j - 1] - '0' == t) {
        return d;
      }
    }
    if (i - 1 >= 0) {
      if (arr[i - 1][j] == '.') {
        if (a[i - 1][j] == 0) {
          a[i - 1][j] = s;
          ss.push({d + 1, m * (i - 1) + j});
        }
      } else if (arr[i - 1][j] - '0' == t) {
        return d;
      }
    }
    if (i + 1 < n) {
      if (arr[i + 1][j] == '.') {
        if (a[i + 1][j] == 0) {
          a[i + 1][j] = s;
          ss.push({d + 1, m * (i + 1) + j});
        }
      } else if (arr[i + 1][j] - '0' == t) {
        return d;
      }
    }
  }
  return (int)1e9;
}
void findmin2(int x) {
  int a[n][m];
  queue<pair<int, int> > ss;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (arr[i][j] - '0' == x + 1) {
        a[i][j] = x + 1;
        ss.push({0, m * i + j});
      } else {
        a[i][j] = 0;
      }
    }
  }
  int i, j, d;
  pair<int, int> top;
  while (!ss.empty()) {
    top = ss.front();
    ss.pop();
    i = top.second / m;
    j = top.second % m;
    d = top.first;
    if (j + 1 < m) {
      if (arr[i][j + 1] == '.' && a[i][j + 1] == 0) {
        a[i][j + 1] = x + 1;
        anst[x][i][j + 1] = d;
        ss.push({d + 1, m * i + j + 1});
      }
    }
    if (j - 1 >= 0) {
      if (arr[i][j - 1] == '.' && a[i][j - 1] == 0) {
        a[i][j - 1] = x + 1;
        anst[x][i][j - 1] = d;
        ss.push({d + 1, m * i + j - 1});
      }
    }
    if (i + 1 < n) {
      if (arr[i + 1][j] == '.' && a[i + 1][j] == 0) {
        a[i + 1][j] = x + 1;
        anst[x][i + 1][j] = d;
        ss.push({d + 1, m * (i + 1) + j});
      }
    }
    if (i - 1 >= 0) {
      if (arr[i - 1][j] == '.' && a[i - 1][j] == 0) {
        a[i - 1][j] = x + 1;
        anst[x][i - 1][j] = d;
        ss.push({d + 1, m * (i - 1) + j});
      }
    }
  }
}
void solve() {
  cin >> n >> m;
  for (int i = 0; i < n; ++i) cin >> arr[i];
  ans[0] = ans[1] = ans[2] = (int)1e9;
  ans[0] = findmin(1, 2);
  ans[1] = findmin(1, 3);
  ans[2] = findmin(2, 3);
  sort(ans, ans + 3);
  long long total = ans[0] + ans[1];
  for (int i = 0; i < 3; ++i)
    for (int j = 0; j < n; ++j)
      for (int k = 0; k < m; ++k) anst[i][j][k] = (int)1e9;
  findmin2(0);
  findmin2(1);
  findmin2(2);
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j)
      if (arr[i][j] == '.') {
        total = min(total,
                    1LL * anst[0][i][j] + anst[1][i][j] + anst[2][i][j] + 1LL);
      }
  if (total >= (int)1e9) total = -1;
  cout << total << endl;
  return;
}
int main() {
  boost();
  int tc = 1;
  while (tc--) solve();
  return 0;
}
