#include <bits/stdc++.h>
using namespace std;
int convert(string s) {
  int a = 0;
  for (int i = 0; i < s.size(); i++) {
    a = 10 * a + (int)(s[i] - 48);
  }
  return a;
}
int main() {
  int i, n, k, c, d, e;
  cin >> n >> k;
  string s, g;
  vector<int> v, v2;
  vector<string> v1;
  int f[k];
  for (i = 0; i < n; i++) {
    cin >> s;
    v1.push_back(s);
    e = convert(s);
    v.push_back(e);
  }
  for (i = 0; i < k; i++) {
    f[i] = i + 1;
  }
  if (n == 1) {
    cout << 0;
    return 0;
  }
  sort(v.begin(), v.end());
  c = v[n - 1] - v[0];
  v.clear();
  while (next_permutation(f, f + k)) {
    for (i = 0; i < n; i++) {
      for (int j = 0; j < k; j++) {
        s[j] = v1[i][f[j] - 1];
      }
      e = convert(s);
      v.push_back(e);
    }
    sort(v.begin(), v.end());
    if (c > v[n - 1] - v[0]) {
      c = v[n - 1] - v[0];
    }
    v.clear();
  }
  cout << c;
  return 0;
}
