#include <bits/stdc++.h>
using namespace std;
signed main() {
  long long n, m;
  cin >> n >> m;
  string a[n];
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
  }
  long long p[m];
  for (long long i = 0; i < m; i++) {
    p[i] = i;
  }
  long long ans = 1e18;
  do {
    long long mini = 1e18;
    long long maxi = 0;
    for (long long i = 0; i < n; i++) {
      long long num = 0;
      for (long long j = 0; j < m; j++) {
        num += (a[i][j] - '0') * pow(10, p[j]);
      }
      mini = min(mini, num);
      maxi = max(maxi, num);
    }
    ans = min(maxi - mini, ans);
  } while (next_permutation(p, p + m));
  cout << ans << endl;
  return 0;
}
