#include <bits/stdc++.h>
using namespace std;
int main() {
  vector<int> a;
  string s;
  int n, k, b[100][100], t, res = 99999999999, min, max;
  cin >> n >> k;
  getline(cin, s);
  for (int i = 0; i < n; i++) {
    getline(cin, s);
    for (int j = 0; j < s.length(); j++) b[i][j] = (int)(s[j] - '0');
  }
  for (int i = 0; i < k; i++) a.push_back(i);
  do {
    min = 99999999999;
    max = 0;
    for (int i = 0; i < n; i++) {
      t = 0;
      for (int j = 0; j < k; j++) t = t * 10LL + b[i][a[j]];
      if (t > max) max = t;
      if (t < min) min = t;
    }
    if (max - min < res) res = max - min;
  } while (next_permutation(a.begin(), a.end()));
  cout << res;
  return 0;
}
