#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, k;
  cin >> n >> k;
  vector<string> vs(n);
  int index[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  for (int i = 0; i < n; ++i) cin >> vs[i];
  int ans = (int)1e9;
  do {
    int nmin = (int)1e9, nmax = -1, num = 0;
    for (int i = 0; i < n; ++i, num = 0) {
      for (int j = 0; j < k; ++j) {
        num = 10 * num + (vs[i][index[j]] - '0');
      }
      nmin = min(nmin, num);
      nmax = max(nmax, num);
    }
    ans = min(ans, nmax - nmin);
  } while (next_permutation(index, index + k));
  cout << ans << "\n";
  return 0;
}
