#include <bits/stdc++.h>
using namespace std;
const int OO = 0x3f3f3f3f;
int main() {
  int n, k;
  cin >> n >> k;
  string s[n];
  int indx[k];
  for (int i = 0; i < n; ++i) cin >> s[i];
  for (int i = 0; i < k; ++i) indx[i] = i;
  long long ans;
  do {
    long long mn = OO, mx = 0;
    for (int i = 0; i < n; ++i) {
      string gg = "";
      for (int j = 0; j < k; ++j) gg += s[i][indx[j]];
      stringstream geek(gg);
      long long x = 0;
      geek >> x;
      mn = min(mn, x);
      mx = max(mx, x);
    }
    ans = min(ans, mx - mn);
  } while (next_permutation(indx, indx + k));
  cout << ans;
  return 0;
}
