#include <bits/stdc++.h>
using namespace std;
int n, m, i, j, k, x, mini, maxi, ans;
int p[100], a[100][100];
int main() {
  cin >> n >> m;
  for (k = 1; k <= n; k++) {
    cin >> x;
    for (i = 1; i <= m; i++) a[k][i] = x % 10, x /= 10;
    for (i = 1; i <= m / 2; i++) swap(a[k][i], a[k][m - i + 1]);
  };
  for (i = 1; i <= m; i++) p[i] = i;
  ans = 1000000001;
  maxi = -1000000001;
  mini = 1000000001;
  for (i = 1; i <= n; i++) {
    x = 0;
    for (j = 1; j <= m; j++) x = x * 10 + a[i][p[j]];
    maxi = max(maxi, x);
    mini = min(mini, x);
  };
  ans = min(ans, maxi - mini);
  while (next_permutation(p + 1, p + m + 1)) {
    maxi = -1000000001;
    mini = 1000000001;
    for (i = 1; i <= n; i++) {
      x = 0;
      for (j = 1; j <= m; j++) x = x * 10 + a[i][p[j]];
      maxi = max(maxi, x);
      mini = min(mini, x);
    };
    ans = min(ans, maxi - mini);
  };
  cout << ans;
  return 0;
}
