#include <bits/stdc++.h>
using namespace std;
vector<int> v;
vector<int> all;
int ans = 1e9, n, m, tmp;
vector<int> arr[10];
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) v.push_back(i);
  string s;
  for (int i = 0; i < n; i++) {
    cin >> s;
    for (int j = 0; j < s.size(); j++) arr[i].push_back(s[j] - '0');
  }
  do {
    all.clear();
    for (int i = 0; i < n; i++) {
      tmp = 0;
      for (int j = 0; j < v.size(); j++) {
        tmp = tmp * 10 + arr[i][v[j]];
      }
      all.push_back(tmp);
    }
    sort(all.begin(), all.end());
    ans = min(ans, all[all.size() - 1] - all[0]);
  } while (next_permutation(v.begin(), v.end()));
  cout << ans << endl;
  return 0;
}
