#include <bits/stdc++.h>
using namespace std;
char a[10][10];
int n, k;
int d[100];
int a2i(char* s) {
  int res = 0;
  for (int i = 0; i < k; i++) {
    res = res * 10 + s[i] - '0';
  }
  return res;
}
int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) cin >> a[i];
  d[0] = 1;
  for (int i = 1; i < 10; i++) {
    d[i] = d[i - 1] * i;
  }
  int index[] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
  int ans = 999999999;
  for (int i = 0; i < d[k]; i++) {
    int ma = 0;
    int mi = 999999999;
    next_permutation(index, index + k);
    for (int kk = 0; kk < n; kk++) {
      char tp[10];
      for (int j = 0; j < k; j++) tp[j] = a[kk][index[j]];
      int ti = a2i(tp);
      ma = max(ma, ti);
      mi = min(mi, ti);
    }
    ans = min(ans, ma - mi);
  }
  cout << ans << endl;
  return 0;
}
