#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  vector<string> a(n);
  for (auto i = a.begin(); i != a.end(); ++i) cin >> *i;
  vector<int> b(k);
  for (int i = 1; i < k; ++i) b[i] = i;
  int mn_dif{1234567890};
  do {
    vector<string> t(a);
    for (int i = 0; i < n; ++i)
      for (int j = 0; j < k; ++j) t[i][b[j]] = a[i][j];
    string mx{t[0]}, mn{t[0]};
    for (int i = 1; i < n; ++i) {
      mx = max(mx, t[i]);
      mn = min(mn, t[i]);
    }
    istringstream imx{mx}, imn{mn};
    int mx_num, mn_num;
    imx >> mx_num;
    imn >> mn_num;
    mn_dif = min(mn_dif, mx_num - mn_num);
  } while (next_permutation(b.begin(), b.end()));
  cout << mn_dif;
}
