#include <bits/stdc++.h>
using namespace std;
string s[8];
int main() {
  int n, k, ans = 1000000000;
  cin >> n >> k;
  for (int i = 0; i < n; ++i) cin >> s[i];
  vector<int> p;
  for (int i = 0; i < k; ++i) p.push_back(i);
  do {
    string cs[8];
    for (int i = 0; i < n; ++i) cs[i] = s[i];
    for (int i = 0; i < k; ++i)
      for (int j = 0; j < n; ++j) cs[j][i] = s[j][p[i]];
    int mx = 0, mn = 1000000000;
    for (int i = 0; i < n; ++i) {
      int val = 0;
      for (int j = 0; j < k; ++j) val = val * 10 + cs[i][j] - '0';
      mx = max(mx, val);
      mn = min(mn, val);
    }
    ans = min(ans, mx - mn);
  } while (next_permutation(p.begin(), p.end()));
  cout << ans << endl;
  return 0;
}
