#include <bits/stdc++.h>
using namespace std;
template <typename T>
string tostr(const T &t) {
  ostringstream os;
  os << t;
  return os.str();
}
const int MAXN = 20, INF = 0x3f3f3f3f;
string B[MAXN];
int toint(vector<int> &pattern, string s) {
  int res;
  string str = s;
  for (int i = 0; i < int((pattern).size()); ++i) str[i] = s[pattern[i]];
  sscanf(str.c_str(), "%d", &res);
  return res;
}
int main(int argc, char *argv[]) {
  vector<int> a;
  int res = INF;
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; ++i) cin >> B[i];
  for (int i = 0; i < k; ++i) a.push_back(i);
  do {
    int lo = INF;
    int hi = 0;
    for (int i = 0; i < n; ++i) {
      int temp = toint(a, B[i]);
      lo = min(lo, temp);
      hi = max(hi, temp);
    }
    res = min(res, hi - lo);
  } while (next_permutation((a).begin(), (a).end()));
  cout << res << endl;
  return 0;
}
