#include <bits/stdc++.h>
using namespace std;
string f(string s, vector<int>& p) {
  string t(s.size(), '0');
  for (int i = 0; i < p.size(); ++i) t[p[i]] = s[i];
  return t;
}
int toInt(string s) {
  int res = 0;
  for (int i = 0; i < s.size(); ++i) {
    res *= 10;
    res += (s[i] - '0');
  }
  return res;
}
int main() {
  int n, k;
  cin >> n >> k;
  vector<string> A(n), B(n);
  vector<int> C(n);
  for (int i = 0; i < n; ++i) cin >> A[i];
  vector<int> p;
  for (int i = 0; i < k; ++i) p.push_back(i);
  int res = 2e9;
  do {
    for (int i = 0; i < n; ++i) B[i] = f(A[i], p);
    for (int i = 0; i < n; ++i) C[i] = toInt(B[i]);
    res = min(res, *max_element(C.begin(), C.end()) -
                       *min_element(C.begin(), C.end()));
  } while (next_permutation(p.begin(), p.end()));
  cout << res;
  return 0;
}
