#include <bits/stdc++.h>
using namespace std;
const int MAXN = 10;
int N, K;
string str[MAXN];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    cin >> str[i];
  }
  vector<int> perm;
  for (int i = 0; i < K; i++) perm.push_back(i);
  int ans = 1000000000;
  do {
    int mnm = 1000000000;
    int mks = 0;
    for (int i = 0; i < N; i++) {
      string result = str[i];
      for (int j = 0; j < K; j++) {
        result[j] = str[i][perm[j]];
      }
      int res_int = stoi(result);
      mnm = min(mnm, res_int);
      mks = max(mks, res_int);
    }
    ans = min(ans, mks - mnm);
  } while (next_permutation(perm.begin(), perm.end()));
  cout << ans << "\n";
}
