#include <bits/stdc++.h>
using namespace std;
const double EPS = 1E-9;
string s[10], t;
long long maxs, mins, temp;
long long n, k, p[10];
long long conv(string a) {
  long long x1 = 0;
  for (int i = 0; i < a.length(); i++) x1 *= 10, x1 += (a[i] - '0');
  return x1;
}
int main() {
  long long max_ans = 100000000 + 13;
  cin >> n >> k;
  for (int i = 0; i < n; i++) cin >> s[i];
  for (int i = 0; i < k; i++) p[i] = i;
  do {
    mins = 100000000 + 13, maxs = 0;
    for (int i = 0; i < n; i++) {
      t = "";
      for (int j = 0; j < k; j++) t += s[i][p[j]];
      temp = conv(t);
      if (mins > temp) mins = temp;
      if (maxs < temp) maxs = temp;
    }
    max_ans = min(max_ans, maxs - mins);
  } while (next_permutation(p, p + k));
  cout << max_ans;
  return 0;
}
