#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const double EPS = 1e-9;
const int oo = 210000000;
int str2int(string x) {
  int l = x.length();
  int ans = 0;
  for (int i = 0; i < l; i++) ans = ans * 10 + (x[i] - 48);
  return ans;
}
int main() {
  int n, k;
  int ans;
  while (cin >> n >> k) {
    string a[10];
    for (int i = 0; i < n; i++) cin >> a[i];
    int b[10];
    int c[10];
    string tmp, tmp1;
    for (int i = 0; i < k; i++) c[i] = i + 1;
    for (int i = 0; i < n; i++) b[i] = str2int(a[i]);
    sort(b, b + n);
    ans = b[n - 1] - b[0];
    do {
      bool flag = 1;
      if (next_permutation(c, c + k)) {
        for (int i = 0; i < n; i++) {
          tmp = tmp1 = a[i];
          for (int j = 0; j < k; j++) tmp[j] = a[i][c[j] - 1];
          a[i] = tmp;
          b[i] = str2int(a[i]);
          a[i] = tmp1;
        }
        sort(b, b + n);
        ans = min(ans, b[n - 1] - b[0]);
      } else {
        flag = 0;
        break;
      }
      if (!flag) break;
    } while (1);
    cout << ans << endl;
  }
  return 0;
}
