#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  string s[n];
  for (int i = 0; i < n; ++i) {
    cin >> s[i];
  }
  vector<int> order(k);
  for (int i = 0; i < k; ++i) {
    order[i] = i;
  }
  int ans = 1e9;
  do {
    string t[n];
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < k; ++j) {
        t[i] += s[i][order[j]];
      }
    }
    int mx = 0, mn = 1e9;
    for (int i = 0; i < n; ++i) {
      mx = max(mx, stoi(t[i]));
      mn = min(mn, stoi(t[i]));
    }
    ans = min(ans, mx - mn);
  } while (next_permutation(order.begin(), order.end()));
  cout << ans << endl;
  return 0;
}
