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