#include <bits/stdc++.h>
using namespace std;
using namespace std;
const long long MOD = 1e9 + 7;
const int INF = 1e9;
int n, k, mn, mx, ans = INF;
int a[8] = {0, 1, 2, 3, 4, 5, 6, 7};
char s[8][10];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> k;
  for (int i = 0; i < n; i++) cin >> s[i];
  int num;
  do {
    mn = 1e9;
    mx = 0;
    for (int i = 0; i < n; i++) {
      num = 0;
      for (int j = 0; j < k; j++) num = num * 10 + (s[i][a[j]] - '0');
      if (mn > num) mn = num;
      if (mx < num) mx = num;
    }
    if (mx - mn < ans) ans = mx - mn;
  } while (next_permutation(a, a + k));
  cout << ans << endl;
  return 0;
}
