#include <bits/stdc++.h>
using namespace std;
int main() {
  int k, n;
  cin >> k >> n;
  string s;
  int t[8][8] = {0};
  cin.ignore();
  for (int i = 0; i < k; i++) {
    getline(cin, s);
    for (int j = 0; j < s.size(); j++) {
      t[i][j] = s[j] - '0';
    }
  }
  int p[] = {1, 2, 3, 4, 5, 6, 7, 8};
  sort(p, p + n);
  int best = 1000000000;
  do {
    int max = 0;
    int min = 1000000000;
    for (int i = 0; i < k; i++) {
      int sum = 0;
      for (int j = 0; j < n; j++) {
        sum *= 10;
        sum += t[i][p[j] - 1];
      }
      if (sum > max) max = sum;
      if (sum < min) min = sum;
    }
    if (best > max - min) best = max - min;
  } while (next_permutation(p, p + n));
  cout << best << endl;
}
