#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10, Msk = (1 << 17), inf = 1e9 + 10, MOD = 1e9 + 7,
          Sq = 708;
const long long INF = 1e18 + 10;
int a[10][10], p[10];
int main() {
  ios_base::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  char ch;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < k; j++) {
      cin >> ch;
      a[i][j] = ch - 48;
    }
  for (int i = 0; i < k; i++) p[i] = i;
  int ans = inf;
  do {
    int mx = -inf, mn = inf;
    for (int i = 0; i < n; i++) {
      int x = 0;
      for (int j = 0; j < k; j++) (x *= 10) += a[i][p[j]];
      mx = max(mx, x);
      mn = min(mn, x);
    }
    ans = min(ans, mx - mn);
  } while (next_permutation(p, p + k));
  cout << ans;
  return 0;
}
