#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  vector<string> nums(n);
  for (int i = (0); i < (int)n; ++i) cin >> nums[i];
  vector<int> seq(k);
  for (int i = (0); i < (int)k; ++i) seq[i] = i;
  int ans = 1e8;
  do {
    int nmax = 0;
    int nmin = 1e8;
    for (int i = (0); i < (int)n; ++i) {
      int number = 0;
      for (int j = (0); j < (int)k; ++j) {
        number = number * 10 + (nums[i][seq[j]] - '0');
      }
      nmax = max(nmax, number);
      nmin = min(nmin, number);
    }
    ans = min(ans, nmax - nmin);
  } while (next_permutation(seq.begin(), seq.end()));
  cout << ans << endl;
  return 0;
}
