#include <bits/stdc++.h>
using namespace std;
int n, k;
char s[10][10], a[10][10];
int d[10];
int p[10];
int main() {
  scanf("%d %d\n", &n, &k);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < k; j++) scanf("%c", &s[i][j]);
    scanf("\n");
  }
  for (int i = 0; i < k; i++) p[i] = i;
  int res = 1000 * 1000 * 1000;
  do {
    int mn = 1000 * 1000 * 1000;
    int mx = -1000 * 1000 * 1000;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < k; j++) a[i][j] = s[i][p[j]];
      d[i] = atoi(a[i]);
      mn = min(mn, d[i]);
      mx = max(mx, d[i]);
    }
    res = min(res, mx - mn);
  } while (next_permutation(p, p + k));
  printf("%d", res);
  return 0;
}
