#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  string s[8];
  vector<int> v;
  for (int i = 0; i < n; cin >> s[i++])
    ;
  for (int i = 0; i < k; i++) v.push_back(i);
  int sum1 = INT_MAX;
  do {
    int mn = INT_MAX, mx = INT_MIN, sum = 0;
    for (int i = 0; i < n; i++) {
      sum = 0;
      for (int j = 0; j < k; j++) {
        sum = sum * 10 + (s[i][v[j]] - '0');
      }
      mn = min(mn, sum);
      mx = max(mx, sum);
    }
    sum1 = min(sum1, (mx - mn));
  } while (next_permutation(v.begin(), v.end()));
  cout << sum1 << endl;
}
