#include <bits/stdc++.h>
using namespace std;
long long power(long long aa, long long pp) {
  long long xx = 1, yy = aa % 1000000007;
  while (pp > 0) {
    if (pp & 1) xx = (xx * yy) % 1000000007;
    yy = (yy * yy) % 1000000007;
    pp = pp >> 1;
  }
  return xx % 1000000007;
}
bool cmp(pair<int, int> a, pair<int, int> b) {
  if (a.first != b.first)
    return a.first < b.first;
  else
    return a.second < b.second;
}
int xc[4] = {0, 1, 0, -1};
int yc[4] = {1, 0, -1, 0};
int main() {
  ios_base::sync_with_stdio(false);
  string s[10];
  int n, k, i, j, ans = (int)1e9, A[10];
  cin >> n >> k;
  for (i = 0; i < n; i++) cin >> s[i];
  for (i = 0; i < k; i++) A[i] = i;
  do {
    int mx = -(int)1e9;
    int mi = (int)1e9;
    for (i = 0; i < n; i++) {
      int sum = 0, po = 1;
      for (j = k - 1; j >= 0; j--) {
        sum += (s[i][A[j]] - '0') * po;
        po = po * 10;
      }
      mx = max(mx, sum);
      mi = min(mi, sum);
    }
    ans = min(ans, mx - mi);
  } while (next_permutation(A, A + k));
  cout << ans;
  return 0;
}
