#include <bits/stdc++.h>
using namespace std;
const int Maxn = 10;
int n, k, cnt[Maxn], anss = INT_MAX;
string arr[Maxn], s, sp;
vector<int> vec;
bool mark[Maxn];
void perm(int ind) {
  if (ind == k) {
    vec.clear();
    for (int i = 0; i < n; i++) {
      long long ans = 0;
      for (int j = 0; j < k; j++) ans *= 10, ans += arr[i][s[j]] - '0';
      vec.push_back(ans);
    }
    sort(vec.begin(), vec.end());
    anss = min(anss, vec[(int)vec.size() - 1] - vec[0]);
    return;
  }
  for (int i = 0; i < k; i++) {
    if (!mark[i]) {
      mark[i] = true;
      s[ind] = i;
      perm(ind + 1);
      mark[i] = false;
    }
  }
  return;
}
int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) cin >> arr[i];
  for (int i = 0; i < k; i++) s += '0', sp += '0';
  perm(0);
  cout << anss << endl;
  return 0;
}
