#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long n;
  cin >> n;
  long long k;
  cin >> k;
  vector<string> q(n), w(n);
  for (long long i = 0; i < n; i++) cin >> q[i];
  w = q;
  vector<long long> ind(k);
  for (long long i = 0; i < k; i++) ind[i] = i;
  long long answer = 100000000;
  do {
    for (long long i = 0; i < n; i++) {
      for (long long j = 0; j < k; j++) {
        w[i][j] = q[i][ind[j]];
      }
    }
    sort(w.begin(), w.end());
    long long l = stoll(w[0]);
    long long r = stoll(w.back());
    answer = min(answer, r - l);
  } while (next_permutation(ind.begin(), ind.end()));
  cout << answer;
  return 0;
}
