#include <bits/stdc++.h>
using namespace std;
string a[10];
int perm[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
int main() {
  int n, k, dif = 1000000000;
  cin >> n >> k;
  for (int i = 0; i < n; i++) cin >> a[i];
  do {
    int m1 = 1000000000;
    int m2 = -1;
    for (int i = 0; i < n; i++) {
      int ans = 0;
      for (int j = 0; j < k; j++) ans = ans * 10 + a[i][perm[j]] - 48;
      m2 = max(ans, m2);
      m1 = min(m1, ans);
    }
    dif = min(dif, (m2 - m1));
  } while (next_permutation(perm, perm + k));
  cout << dif;
  return 0;
}
