#include <bits/stdc++.h>
using namespace std;
int order[8];
char num[8][9];
int calc(int n, int k) {
  int i, j, deg, mn = 100000000, mx = 0, tmp;
  for (i = 0; i < n; ++i) {
    for (tmp = 0, deg = 1, j = k - 1; j >= 0; --j) {
      tmp += deg * (num[i][order[j]] - '0');
      deg *= 10;
    }
    if (tmp < mn) mn = tmp;
    if (mx < tmp) mx = tmp;
  }
  return mx - mn;
}
int BF(int i, int n, int k) {
  if (i == k - 1) return calc(n, k);
  int j, ans = 100000000;
  for (j = i + 1; j < k; ++j) {
    ans = min(ans, BF(i + 1, n, k));
    swap(order[i], order[j]);
    ans = min(ans, BF(i + 1, n, k));
    swap(order[i], order[j]);
  }
  return ans;
}
int main() {
  int n, k, i;
  scanf("%d%d", &n, &k);
  for (i = 0; i < n; ++i) scanf("%s", num[i]);
  for (i = 0; i < k; ++i) order[i] = i;
  printf("%d\n", BF(0, n, k));
  return 0;
}
