#include <bits/stdc++.h>
using namespace std;
const int inf = 0x5fffffff;
char num[10][10];
int a[10];
int jii[10];
void init() {
  a[0] = 1;
  jii[0] = 1;
  for (int i = 1; i < 10; ++i) {
    a[i] = a[i - 1] * 10;
    jii[i] = jii[i - 1] * i;
  }
}
int main() {
  int n, k;
  while (~scanf("%d%d", &n, &k)) {
    init();
    int tmp;
    int ans = inf;
    int cnt = jii[k];
    for (int i = 0; i < n; ++i) {
      scanf("%s", num[i]);
    }
    for (int i = 0; i < cnt; ++i) {
      int mins = inf;
      int maxs = -10;
      for (int j = 0; j < n; ++j) {
        tmp = 0;
        for (int p = 0; p < k; ++p) {
          tmp += a[p] * (num[j][p] - '0');
        }
        if (tmp > maxs) maxs = tmp;
        if (tmp < mins) mins = tmp;
      }
      next_permutation(a, a + k);
      ans = min(maxs - mins, ans);
    }
    printf("%d\n", ans);
  }
  return 0;
}
