#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  while (scanf("%d%d", &n, &k) != EOF) {
    char s[10][10], str[10][10];
    for (int i = 0; i < n; i++) {
      scanf("%s", str[i]);
    }
    int a, i;
    int mn = 1000000000;
    int tmn, tmx;
    int perm[10];
    for (i = 0; i < k; i++) perm[i] = i;
    do {
      for (i = 0; i < n; i++) {
        char tmp[10];
        tmp[k] = '\0';
        for (int j = 0; j < k; j++) tmp[j] = str[i][perm[j]];
        strcpy(s[i], tmp);
      }
      tmn = 1000000000;
      tmx = 0;
      for (i = 0; i < n; i++) {
        a = atoi(s[i]);
        if (a < tmn) tmn = a;
        if (a > tmx) tmx = a;
      }
      if (tmx - tmn < mn) mn = tmx - tmn;
    } while (next_permutation(perm, perm + k));
    cout << mn << endl;
  }
  return 0;
}
