#include <bits/stdc++.h>
using namespace std;
char q[15][15];
int np[15], ans, n, k;
void perm(int kk, int nn) {
  if (kk == nn - 1) {
    char s[15];
    int i, j, tmp, max_v, min_v, pos;
    max_v = -1;
    min_v = 1000000000;
    for (i = 0; i < n; ++i) {
      tmp = 0;
      for (j = 0; j < k; ++j) {
        if (q[i][np[j]] != 0) break;
      }
      pos = j;
      for (j = k - 1; j >= pos; --j) tmp = tmp * 10 + q[i][np[j]] - '0';
      max_v = max(max_v, tmp);
      min_v = min(min_v, tmp);
    }
    ans = min(ans, max_v - min_v);
  } else {
    for (int i = kk; i < nn; ++i) {
      swap(np[kk], np[i]);
      perm(kk + 1, nn);
      swap(np[kk], np[i]);
    }
  }
}
int main() {
  int i;
  while (scanf("%d %d", &n, &k) != EOF) {
    for (i = 0; i < n; ++i) scanf("%s", q + i);
    for (i = 0; i < k; ++i) np[i] = i;
    ans = 1000000000;
    perm(0, k);
    printf("%d\n", ans);
  }
  return 0;
}
