#include <bits/stdc++.h>
using namespace std;
int n, k;
char str[10][10];
int a[10], ans, p[10];
int main() {
  cin >> n >> k;
  for (int i = 0; i < n; ++i) cin >> str[i];
  for (int i = 0; i < k; ++i) a[i] = i;
  ans = ~0U >> 3;
  do {
    memset(p, 0, sizeof(p));
    for (int i = 0; i < n; ++i)
      for (int j = 0; j < k; ++j) p[i] = p[i] * 10 + str[i][a[j]] - '0';
    sort(p, p + n);
    ans = min(ans, p[n - 1] - p[0]);
  } while (next_permutation(a, a + k));
  cout << ans << endl;
  return 0;
}
