#include <bits/stdc++.h>
using namespace std;
int main() {
  char str[8][10];
  int n, k, i, j, l, mx, mn, mind;
  int p[] = {1, 2, 3, 4, 5, 6, 7, 8};
  cin >> n >> k;
  for (i = 0; i < n; i++) cin >> str[i];
  mx = 0;
  mn = 1000000000;
  mind = mn - mx;
  do {
    int arr[8] = {0};
    mx = 0;
    mn = 1000000000;
    for (i = 0; i < n; i++) {
      for (j = 0; j < k; j++) {
        arr[i] *= 10;
        arr[i] += (int)(str[i][p[j] - 1] - '0');
      }
      if (arr[i] > mx) mx = arr[i];
      if (arr[i] < mn) mn = arr[i];
    }
    mind = min(mind, (mx - mn));
  } while (std::next_permutation(p, p + k));
  cout << mind << endl;
  return 0;
}
