#include <bits/stdc++.h>
using namespace std;
int ar[40325][10];
int n, d, i, j, m, k, ac, mn, mx, ans;
char str[10];
void swap(char *x, char *y) {
  char temp;
  temp = *x;
  *x = *y;
  *y = temp;
}
void permute(char *a, int l, int r) {
  int i;
  if (l == r) {
    m = a[0] - '0';
    for (k = 1; k < strlen(a); k++) {
      m = (m * 10) + ((a[k] - '0') % 10);
    }
    ar[ac++][j] = m;
  } else {
    for (i = l; i <= r; i++) {
      swap((a + l), (a + i));
      permute(a, l + 1, r);
      swap((a + l), (a + i));
    }
  }
}
int main() {
  cin >> n >> d;
  for (j = 0; j < n; j++) {
    ac = 0;
    cin >> str;
    permute(str, 0, d - 1);
  }
  ans = 1000000000;
  for (i = 0; i < ac; i++) {
    mx = 0;
    mn = 1000000000;
    for (j = 0; j < n; j++) {
      mn = min(mn, ar[i][j]);
      mx = max(mx, ar[i][j]);
    }
    ans = min(ans, (mx - mn));
  }
  cout << ans << endl;
}
