#include <bits/stdc++.h>
using namespace std;
long long n, k;
vector<string> vs;
vector<long long> vi;
bool visited[9];
long long res = 100000000;
void dfs(long long depth) {
  if (depth == 0) {
    long long maxi = 0, mini = 100000000;
    for (long long i = 0; i < n; ++i) {
      long long tmp = 0;
      ;
      for (long long j = 0; j < k; ++j) {
        tmp *= 10;
        tmp += vs[i][vi[j]] - '0';
      }
      maxi = max(maxi, tmp);
      mini = min(mini, tmp);
    }
    res = min(res, maxi - mini);
    return;
  }
  for (long long i = 0; i < k; ++i) {
    if (!visited[i]) {
      visited[i] = true;
      vi.push_back(i);
      dfs(depth - 1);
      vi.pop_back();
      visited[i] = false;
    }
  }
}
int main() {
  cin >> n >> k;
  for (long long i = 0; i < n; ++i) {
    string tmp;
    cin >> tmp;
    vs.push_back(tmp);
  }
  dfs(k);
  cout << res << endl;
  return 0;
}
