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