#include <bits/stdc++.h>
using namespace std;
int n, k;
char str[100][100];
int per[100];
int main() {
  scanf("%d %d", &n, &k);
  for (int i = 0; i < n; i++) scanf("%s", str[i]);
  for (int i = 0; i < k; i++) per[i] = i;
  int ans = 100000000;
  do {
    int nMax = -1, nMin = 1000000000;
    for (int i = 0; i < n; i++) {
      int tmp = 0;
      for (int j = 0; j < k; j++) {
        tmp = tmp * 10 + str[i][per[j]] - '0';
      }
      nMax = max(nMax, tmp), nMin = min(nMin, tmp);
    }
    ans = min(ans, nMax - nMin);
  } while (next_permutation(per, per + k));
  printf("%d\n", ans);
  return 0;
}
