#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  char s[10][10];
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n; i++) scanf("%s", s[i]);
  int p[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
  int ans = 99999999;
  do {
    char tmps[10];
    int Min = 99999999, tmp, Max = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < k; j++) tmps[j] = s[i][p[j]];
      tmps[k] = '\0';
      sscanf(tmps, "%d", &tmp);
      Min = min(Min, tmp);
      Max = max(Max, tmp);
    }
    ans = min(ans, Max - Min);
  } while (next_permutation(p, p + k));
  printf("%d\n", ans);
  return 0;
}
