#include <bits/stdc++.h>
int main() {
  int n, k;
  std::cin >> n >> k;
  std::string s[8];
  for (int i = 0; i < n; ++i) {
    std::cin >> s[i];
    for (int j = 0; j < s[i].size(); ++j) {
      s[i][j] -= '0';
    }
  }
  int p[8];
  for (int i = 0; i < k; ++i) p[i] = i;
  int ans = 100000000;
  do {
    int min = 100000000, max = -1;
    for (int i = 0; i < n; ++i) {
      int acc = 0;
      for (int j = 0; j < k; ++j) {
        acc = acc * 10 + s[i][p[j]];
      }
      min = std::min(acc, min);
      max = std::max(acc, max);
    }
    ans = std::min(max - min, ans);
  } while (std::next_permutation(p, p + k));
  std::cout << ans;
}
