#include <bits/stdc++.h>
using namespace std;
int main() {
  const int INF = 1 << 30;
  int n, k;
  while (cin >> n >> k) {
    vector<string> digit(n);
    for (int i = 0; i < n; ++i) cin >> digit[i];
    vector<int> index;
    for (int i = 0; i < k; ++i) index.push_back(i);
    int ans = INF;
    do {
      int d_max = 0, d_min = INF;
      for (int i = 0; i < n; ++i) {
        int t = 0;
        for (int j = 0; j < k; ++j) {
          t *= 10;
          t += (digit[i][index[j]] - '0');
        }
        if (t > d_max) d_max = t;
        if (t < d_min) d_min = t;
      }
      ans = min(ans, d_max - d_min);
    } while (next_permutation(index.begin(), index.end()));
    cout << ans << endl;
  }
  return 0;
}
