#include <bits/stdc++.h>
using namespace std;
const int oo = (int)1e9;
const double PI = 2 * acos(0.0);
const double eps = 1e-9;
int getint(string s) {
  int ans;
  sscanf(s.c_str(), "%d", &ans);
  return ans;
}
int main() {
  int n, len;
  cin >> n >> len;
  vector<string> vs(n);
  for (int u = 0; u < n; ++u) cin >> vs[u];
  vector<int> perm;
  for (int i = 0; i < len; ++i) perm.push_back(i);
  int ans = oo;
  do {
    vector<string> nvs;
    for (int i = 0; i < (int)vs.size(); ++i) {
      string ns = vs[i];
      for (int k = 0; k < len; ++k) ns[k] = vs[i][perm[k]];
      nvs.push_back(ns);
    }
    int mn = oo, mx = -1;
    for (int i = 0; i < (int)nvs.size(); ++i) {
      mn = min(mn, getint(nvs[i]));
      mx = max(mx, getint(nvs[i]));
    }
    ans = min(ans, abs(mx - mn));
  } while (next_permutation(perm.begin(), perm.end()));
  cout << ans << endl;
  return 0;
}
