#include <bits/stdc++.h>
using namespace std;
const int maxn = (int)1e5 + 5;
int main() {
  int n, k;
  char buf[8][12];
  cin >> n >> k;
  for (int i = 0; i < n; ++i) {
    cin >> buf[i];
  }
  int result = 0x3f3f3f3f;
  int idx[8] = {0, 1, 2, 3, 4, 5, 6, 7};
  do {
    int minVal = 0x3f3f3f3f;
    int maxVal = 0;
    for (int i = 0; i < n; ++i) {
      int num = 0;
      for (int j = 0; j < k; ++j) {
        num = num * 10 + buf[i][idx[j]] - '0';
      }
      minVal = min(minVal, num);
      maxVal = max(maxVal, num);
    }
    result = min(result, maxVal - minVal);
  } while (next_permutation(idx, idx + k));
  cout << result << endl;
  return 0;
}
