#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-9;
const long long INF = 1000000000000000000;
int n, m;
bool check(int x, int y) { return (x >= 0 && x < n && y >= 0 && y < n); }
int ss(string s) {
  int res = 0;
  for (int _n(s.size()), i(0); i < _n; i++) {
    res = res * 10 + (int)(s[i] - '0');
  }
  return res;
}
int main() {
  cin >> n >> m;
  vector<string> v(n);
  for (int _n(n), i(0); i < _n; i++) cin >> v[i];
  vector<int> perm(m);
  for (int _n(m), i(0); i < _n; i++) perm[i] = i;
  long long res = 10000000000;
  do {
    vector<string> curr(n);
    for (int _n(n), i(0); i < _n; i++) {
      string t = v[i];
      for (int _n(m), j(0); j < _n; j++) {
        t[j] = v[i][perm[j]];
      }
      curr[i] = t;
    }
    sort((curr).begin(), (curr).end());
    long long tmp = abs(ss(curr[0]) - ss(curr[curr.size() - 1]));
    if (tmp < res) res = tmp;
  } while (next_permutation(perm.begin(), perm.end()));
  cout << res << endl;
}
