#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:100000000000,100000000000")
const long long INF = 2009000999;
const double cp = 2 * asin(1.0);
const double eps = 1e-9;
const long long mod = 1000000007LL;
using namespace std;
int main() {
  int a[101][101];
  int n, k;
  int p[101];
  int ans;
  cin >> n >> k;
  for (int(i) = 0; (i) < (n); i++) {
    for (int(j) = 0; (j) < (k); j++) {
      char c;
      cin >> c;
      a[i][j] = c - '0';
    }
  }
  for (int(i) = 0; (i) < (k); i++) p[i] = i;
  ans = INF;
  do {
    int mi = INF, ma = -INF;
    for (int i = 0; i < n; i++) {
      int x = 0;
      for (int j = 0; j < k; j++) (x *= 10) += a[i][p[j]];
      ma = max(ma, x);
      mi = min(mi, x);
    }
    ans = min(ans, ma - mi);
  } while (next_permutation(p, p + k));
  cout << ans;
  return 0;
}
