#include <bits/stdc++.h>
using namespace std;
const double kEps = 10e-8;
const int kMax = 1000;
const int kInf = 1 << 30;
int main() {
  int n, k;
  cin >> n >> k;
  vector<string> numbers;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    numbers.push_back(s);
  }
  vector<int> idx;
  for (int i = 0; i < k; i++) {
    idx.push_back(i);
  }
  int ans = kInf;
  do {
    int mx = 0;
    int mn = kInf;
    for (string x : numbers) {
      int v = 0;
      for (int j = 0; j < k; j++) {
        char c = x[idx[j]];
        v = v * 10 + c - '0';
      }
      mx = max(mx, v);
      mn = min(mn, v);
    }
    ans = min(ans, mx - mn);
  } while (next_permutation(idx.begin(), idx.end()));
  cout << ans << endl;
  return EXIT_SUCCESS;
}
