#include <bits/stdc++.h>
using namespace std;
long long n, k, ans = INT64_MAX;
long long v[10];
char s[9][9];
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.precision(10), cout << fixed;
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n; ++i) scanf("%s", &s[i]);
  for (int i = 0; i < k; ++i) v[i] = i;
  do {
    long long mx = -1, mn = INT_MAX;
    for (int i = 0; i < n; ++i) {
      long long Q = 0;
      for (int j = 0; j < k; ++j) Q = Q * 10 + (s[i][v[j]] - '0');
      mx = max(mx, Q);
      mn = min(mn, Q);
    }
    ans = min(ans, mx - mn);
  } while (next_permutation(v, v + k));
  printf("%lld\n", ans);
  return 0;
}
