#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
const double pi = acos(-1.0);
int dblcmp(double d) {
  if (fabs(d) < eps) return 0;
  return d > eps ? 1 : -1;
}
string s[10];
int main() {
  int a[10], b, n, p[10];
  int i, j, k, ans = 1111111111;
  scanf("%d%d", &n, &k);
  for (i = 0; i < n; i++) {
    cin >> s[i];
  }
  for (i = 0; i < 10; i++) p[i] = i;
  do {
    for (i = 0; i < n; i++) {
      string t = "";
      for (j = 0; j < k; j++) t += s[i][p[j]];
      a[i] = atoi(t.c_str());
    }
    sort(a, a + n);
    ans = min(ans, a[n - 1] - a[0]);
  } while (next_permutation(p, p + k));
  printf("%d\n", ans);
  return 0;
}
