#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long n, k, ans = LLONG_MAX;
  cin >> n >> k;
  vector<string> s(n);
  for (string &i : s) cin >> i;
  vector<long long> dummy(k);
  for (long long i = 0; i < k; i++) dummy[i] = i;
  do {
    long long mx = -1, mn = LLONG_MAX;
    for (string x : s) {
      string temp;
      for (long long i = 0; i < k; i++) temp += x[dummy[i]];
      mx = max(mx, stoll(temp));
      mn = min(mn, stoll(temp));
    }
    ans = min(ans, mx - mn);
  } while (next_permutation(dummy.begin(), dummy.end()));
  cout << ans << '\n';
  return 0;
}
