#include <bits/stdc++.h>
using namespace std;
string a[100];
int n, k;
int p[100];
int getNum(int i) {
  int x = 0;
  for (int j = 0; j < k; ++j) {
    x = x * 10 + a[i][p[j]] - '0';
  }
  return x;
}
int main() {
  cin >> n >> k;
  for (int i = 0; i < (n); i++) cin >> a[i];
  for (int i = 0; i < (k); i++) p[i] = i;
  int res = 1e9;
  do {
    int mx = getNum(0);
    int mn = getNum(0);
    for (int i = (1); i <= (n - 1); i++) {
      int x = getNum(i);
      mn = min(mn, x);
      mx = max(mx, x);
    }
    res = min(res, mx - mn);
  } while (next_permutation(p, p + k));
  cout << res << endl;
  return 0;
}
