#include <bits/stdc++.h>
using namespace std;
int n, k;
char s[8][10];
int i;
vector<int> p;
int cnt() {
  int v[8];
  int i, j;
  for (i = 0; i < n; i++) {
    v[i] = 0;
    for (j = 0; j < k; j++) v[i] = v[i] * 10 + (int)(s[i][p[j]] - '0');
  }
  int minv = v[0];
  int maxv = v[0];
  for (i = 0; i < n; i++) {
    if (v[i] > maxv) maxv = v[i];
    if (v[i] < minv) minv = v[i];
  }
  return maxv - minv;
}
int main() {
  scanf("%d %d", &n, &k);
  for (i = 0; i < n; i++) scanf("%s", s[i]);
  for (i = 0; i < k; i++) p.push_back(i);
  int sol = cnt();
  while (next_permutation(p.begin(), p.end())) {
    int res = cnt();
    if (res < sol) sol = res;
  }
  printf("%d\n", sol);
  return 0;
}
