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