#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  string s[n];
  for (int i = 0; i < n; i++) cin >> s[i];
  long long ans = 0x3f3f3f3f3f3f3f3fll;
  int v[k];
  for (int i = 0; i < k; i++) v[i] = i;
  do {
    long long mi = 1000000000000000ll, ma = 0;
    for (int i = 0; i < n; i++) {
      long long num = 0;
      for (int j = 0; j < k; j++) {
        num *= 10;
        num += (s[i][v[j]] - '0');
      }
      mi = min(mi, num);
      ma = max(ma, num);
    }
    ans = min(ans, ma - mi);
  } while (next_permutation(v, v + k));
  cout << ans << endl;
  return 0;
}
