#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<int> per;
string s[10];
int solve() {
  int mmin = 0x3f3f3f3f;
  int mmax = -0x3f3f3f3f;
  string ss;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      ss[j] = s[i][per[j]];
    }
    int t = atoi(ss.c_str());
    mmin = min(t, mmin);
    mmax = max(t, mmax);
  }
  return abs(mmax - mmin);
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) cin >> s[i];
  for (int i = 0; i < m; i++) per.push_back(i);
  int ans = 0x3f3f3f3f;
  do {
    ans = min(ans, solve());
  } while (next_permutation(per.begin(), per.end()));
  printf("%d\n", ans);
  return 0;
}
