#include <bits/stdc++.h>
using namespace std;
int n, k, per[10];
string a[10];
inline int change(string);
int main() {
  std::ios::sync_with_stdio(false);
  cin >> n >> k;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i <= k; i++) per[i] = i;
  int res = 1e9;
  do {
    int maxi = 0, mini = 1 << 27;
    for (int i = 1; i <= n; i++) {
      int temp = change(a[i]);
      maxi = max(maxi, temp);
      mini = min(mini, temp);
    }
    res = min(res, maxi - mini);
  } while (next_permutation(per + 1, per + k + 1));
  cout << res;
  return 0;
}
inline int change(string s) {
  char c[10];
  for (int i = 0; i < k; i++) c[i] = s[per[i + 1] - 1];
  int ret = atoi(c);
  return ret;
}
