#include <bits/stdc++.h>
using namespace std;
int it(string s) {
  int ans = 0;
  for (int i = 0; i < s.size(); i++) {
    ans += int(s[i] - '0');
    if (i != s.size() - 1) ans *= 10;
  }
  return ans;
}
int main() {
  int n, m, ar[10], ar2[10];
  cin >> n >> m;
  string s[10];
  for (int i = 0; i < n; i++) {
    cin >> s[i];
  }
  for (int i = 0; i < m; i++) {
    ar[i] = i;
  }
  int mn = INT_MAX, ans;
  string s2[10];
  bool b = 0;
  for (int i = 0; i < n; i++) {
    ar2[i] = it(s[i]);
  }
  sort(ar2, ar2 + n);
  ans = ar2[n - 1] - ar2[0];
  mn = min(mn, ans);
  while (next_permutation(ar, ar + m)) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        s2[i].push_back(s[i][ar[j]]);
      }
    }
    for (int i = 0; i < n; i++) {
      ar2[i] = it(s2[i]);
    }
    sort(ar2, ar2 + n);
    ans = ar2[n - 1] - ar2[0];
    mn = min(mn, ans);
    for (int i = 0; i < n; i++) {
      s2[i].clear();
    }
  }
  cout << mn << endl;
  return 0;
}
