#include <bits/stdc++.h>
using namespace std;
int fact(int k) { return k > 1 ? k * fact(k - 1) : 1; }
bool solve(int& tc) {
  int n, k;
  cin >> n >> k;
  string* ins = new string[n];
  for (int i = (0); i < int(n); ++i) cin >> ins[i];
  vector<int> perm;
  for (int i = (0); i < int(k); ++i) perm.push_back(i);
  int ans = 1000000000;
  int kfact = fact(k);
  for (int i = (0); i < int(kfact); ++i) {
    int maxnum = 0;
    int minnum = 1000000000;
    for (int j = (0); j < int(n); ++j) {
      int num = 0;
      int p = 1;
      for (int ii = k - 1; ii >= 0; --ii) {
        num += (ins[j][perm[ii]] - '0') * p;
        p *= 10;
      }
      maxnum = max(maxnum, num);
      minnum = min(minnum, num);
    }
    ans = min(ans, maxnum - minnum);
    next_permutation(perm.begin(), perm.end());
  }
  cout << ans << endl;
  return false;
}
int main() {
  int n = 1 << 30;
  if (!n) cin >> n;
  for (int i = 0; i < n && solve(i); ++i)
    ;
  return 0;
}
