#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  int ch[n][k];
  for (int i = 0; i < n; i++) {
    int l;
    cin >> l;
    for (int j = k - 1; j >= 0; j--) {
      ch[i][j] = l % 10;
      l /= 10;
    }
  }
  vector<int> perm(k);
  for (int i = 0; i < k; i++) {
    perm[i] = i;
  }
  int ans = 1000000000;
  do {
    int newmin = 1000000000, newmax = 0;
    for (int i = 0; i < n; i++) {
      int l = 0;
      for (int j = 0; j < k; j++) {
        l *= 10;
        l += ch[i][perm[j]];
      }
      newmax = max(newmax, l);
      newmin = min(l, newmin);
    }
    ans = min(ans, newmax - newmin);
  } while (next_permutation(perm.begin(), perm.end()));
  cout << ans << endl;
  return 0;
}
