#include <bits/stdc++.h>
using namespace std;
char A[11][11];
int perm[11];
int n, k;
int s[11];
int res() {
  int i, j;
  memset(s, 0, sizeof(s));
  for (i = 0; i < n; i++) {
    for (j = 0; j < k; j++) {
      s[i] = s[i] * 10 + (A[i][perm[j]] - '0');
    }
  }
  int minV = 0x3F3F3F3F;
  int maxV = 0;
  for (i = 0; i < n; i++) {
    minV = min(minV, s[i]);
    maxV = max(maxV, s[i]);
  }
  return abs(minV - maxV);
}
int main() {
  int i;
  for (i = 0; i < 11; i++) perm[i] = i;
  scanf("%d %d%*c", &n, &k);
  for (i = 0; i < n; i++) gets(A[i]);
  int minV = 0x3F3F3F3F;
  do {
    minV = min(minV, res());
  } while (next_permutation(perm, perm + k));
  cout << minV << endl;
}
