#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k_d;
  cin >> n >> k_d;
  vector<string> ov(n), cv(n);
  vector<int> prem(k_d);
  for (int i = 0; i < n; i++) {
    cin >> ov[i];
    cv[i] = ov[i];
  }
  for (int i = 0; i < k_d; i++) prem[i] = i;
  string mns, mxs;
  int mx = 0, mn = 0, res = 2e9;
  do {
    cv = ov;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < k_d; j++) {
        cv[i][j] = ov[i][prem[j]];
      }
    }
    mxs = *max_element(cv.begin(), cv.end());
    mns = *min_element(cv.begin(), cv.end());
    mn = atoi(mns.c_str());
    mx = atoi(mxs.c_str());
    res = min(res, mx - mn);
  } while (next_permutation(prem.begin(), prem.end()));
  cout << res;
}
