#include <bits/stdc++.h>
using namespace std;
const int maxn = 9;
const int maxk = 9;
int n, k;
char s[maxn][maxk];
int a[maxn], num[maxn];
int main() {
  while (scanf("%d%d", &n, &k) != EOF) {
    for (int i = 0; i < n; i++) scanf("%s", s[i]);
    for (int i = 0; i < k; i++) a[i] = i;
    int ans = 1000000000;
    do {
      for (int i = 0; i < n; i++) {
        num[i] = 0;
        for (int j = 0; j < k; j++) num[i] = num[i] * 10 + s[i][a[j]] - '0';
      }
      sort(num, num + n);
      ans = min(abs(num[0] - num[n - 1]), ans);
    } while (next_permutation(a, a + k));
    printf("%d\n", ans);
  }
  return 0;
}
