#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  vector<vector<int> > a(n, vector<int>(k, 0));
  for (int i = 0; i < n; i++) {
    int b;
    cin >> b;
    for (int j = k - 1; j >= 0; j--) {
      a[i][j] = b % 10;
      b /= 10;
    }
  }
  vector<int> p;
  for (int i = 0; i < k; i++) {
    p.push_back(i);
  }
  sort(p.begin(), p.end());
  int md = 1000000000;
  do {
    int mi = 1000000000;
    int ma = 0;
    for (int i = 0; i < n; i++) {
      int num = 0;
      for (int j = 0; j < k; j++) {
        num = (num * 10) + a[i][p[j]];
      }
      mi = min(mi, num);
      ma = max(ma, num);
    }
    int diff = ma - mi;
    md = min(diff, md);
  } while (next_permutation(p.begin(), p.end()));
  cout << md << endl;
  return 0;
}
