#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long n, k;
  cin >> n >> k;
  string s[n];
  long long i, j;
  for (i = 0; i < n; i++) cin >> s[i];
  vector<int> perm;
  for (i = 0; i < k; i++) perm.push_back(i);
  long long ans = 1e18, num, mx = -1, mn = 1e18;
  string mods[n];
  do {
    for (i = 0; i < n; i++) mods[i] = string();
    for (i = 0; i < n; i++)
      for (j = 0; j < k; j++) mods[i].push_back(s[i][perm[j]]);
    mx = -1, mn = 1e18;
    for (i = 0; i < n; i++) {
      num = stoi(mods[i]);
      mx = max(mx, num);
      mn = min(mn, num);
    }
    ans = min(ans, mx - mn);
  } while (next_permutation(perm.begin(), perm.end()));
  cout << ans << endl;
}
