#include <bits/stdc++.h>
using namespace std;
int n, l, mn = 1e9;
vector<bool> valido;
vector<int> orden;
vector<string> num;
void bt(int a) {
  if (a == l) {
    int k = 1e9, p = -1;
    for (auto s : num) {
      int j = 0;
      for (int y = 0; y < l; y++) {
        int fd = int(round(pow(10, y)));
        j += fd * (s[orden[y]] - 48);
      }
      k = min(k, j);
      p = max(p, j);
    }
    mn = min(mn, p - k);
    return;
  }
  for (int x = 0; x < l; x++) {
    if (valido[x]) {
      valido[x] = 0;
      orden[a] = x;
      bt(a + 1);
      valido[x] = 1;
    }
  }
}
int main() {
  cin >> n >> l;
  valido.assign(l, 1);
  orden.assign(l, -1);
  num.assign(n, "bob");
  for (int x = 0; x < n; x++) {
    cin >> num[x];
  }
  bt(0);
  cout << mn << "\n";
}
