#include <bits/stdc++.h>
using namespace std;
const int OO = 0x3f3f3f3f, N = 1e5 + 5, mod = 1e9 + 9;
int n, k, indx[10], ans = OO;
char arr[10][10];
int num(int r) {
  int ret = 0;
  for (int i = 0; i < k; ++i) {
    ret = ret * 10 + (arr[r][indx[i]] - '0');
  }
  return ret;
}
int main() {
  cin >> n >> k;
  for (int i = 0; i < k; ++i) indx[i] = i;
  for (int i = 0; i < n; ++i) scanf("%s", arr[i]);
  do {
    int mn = OO, mx = 0;
    for (int i = 0; i < n; ++i) {
      mn = min(mn, num(i));
      mx = max(mx, num(i));
    }
    ans = min(ans, mx - mn);
  } while (next_permutation(indx, indx + k));
  cout << ans;
  return 0;
}
