#include <bits/stdc++.h>
using namespace std;
int n, k, ans = 0xfffffff;
std::vector<string> v;
string s;
int to_intx(string a) {
  int cnt = 0;
  int ans = 0;
  for (int i = a.size() - 1; i >= 0; --i) {
    ans += (a[i] - '0') * pow(10, cnt);
    ++cnt;
  }
  return ans;
}
int main() {
  cin >> n >> k;
  for (int i = 0; i < k; ++i) {
    s += to_string(i);
  }
  v.resize(n);
  for (int i = 0; i < n; ++i) cin >> v[i];
  string minx = "9999999999999", maxn = "/";
  for (int i = 0; i < n; ++i) {
    string temp = "";
    for (int j = 0; j < s.size(); ++j) {
      temp += " ";
      int index = s[j] - '0';
      temp[temp.size() - 1] = v[i][index];
    }
    minx = min(minx, temp);
    maxn = max(maxn, temp);
  }
  ans = min(ans, abs(to_intx(minx) - to_intx(maxn)));
  while (next_permutation(s.begin(), s.end())) {
    string minx = "9999999999999", maxn = "/";
    for (int i = 0; i < n; ++i) {
      string temp = "";
      for (int j = 0; j < s.size(); ++j) {
        temp += " ";
        int index = s[j] - '0';
        temp[temp.size() - 1] = v[i][index];
      }
      minx = min(minx, temp);
      maxn = max(maxn, temp);
    }
    ans = min(ans, abs(to_intx(minx) - to_intx(maxn)));
  }
  cout << ans;
}
