#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  cin.get();
  vector<string> v(n);
  for (int i = 0; i < n; i++) getline(cin, v[i]);
  vector<int> p(k);
  for (int i = 0; i < k; i++) p[i] = i;
  int minDiff = INT_MAX;
  do {
    int minNum, maxNum;
    minNum = INT_MAX;
    maxNum = INT_MIN;
    for (int i = 0; i < n; i++) {
      int num = 0;
      for (int j = 0; j < k; j++) num = num * 10 + v[i][p[j]] - '0';
      if (num < minNum) minNum = num;
      if (num > maxNum) maxNum = num;
    }
    minDiff = min(minDiff, maxNum - minNum);
  } while (next_permutation(p.begin(), p.end()));
  cout << minDiff;
  return 0;
}
