#include <bits/stdc++.h>
using namespace std;
vector<long long> modify(vector<long long> pos, vector<string> a) {
  vector<long long> ans;
  for (long long i = 0; i < (long long)(a).size(); ++i) {
    string s = a[i];
    for (long long j = 0; j < (long long)(pos).size(); ++j) {
      s[j] = a[i][pos[j]];
    }
    long long num = stoll(s);
    ans.push_back(num);
  }
  return ans;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long n, k, an = LLONG_MAX;
  cin >> n >> k;
  vector<long long> v;
  for (long long i = 0; i < k; ++i) v.push_back(i);
  vector<vector<long long> > ans;
  do {
    ans.push_back(v);
  } while (next_permutation((v).begin(), (v).end()));
  vector<string> a(n);
  for (auto& xy : a) cin >> xy;
  for (auto& x : ans) {
    vector<string> real = a;
    vector<long long> change = modify(x, real);
    an = min(an, *max_element((change).begin(), (change).end()) -
                     *min_element((change).begin(), (change).end()));
  }
  cout << an;
}
