#include <bits/stdc++.h>
using namespace std;
int n, k;
int a[10][10];
int b[10];
char c[10];
int main() {
  cin >> n >> k;
  for (int i = 0; i < k; ++i) b[i] = i;
  for (int i = 0; i < n; ++i) {
    cin >> c;
    for (int j = 0; j < k; ++j) a[i][j] = c[j] - '0';
  }
  sort(b, b + k);
  int best = (1 << 30);
  do {
    int mmax = 0;
    int mmin = (1 << 30);
    for (int i = 0; i < n; ++i) {
      int sum = 0;
      for (int j = 0; j < k; ++j) {
        sum = sum * 10 + a[i][b[j]];
      }
      if (sum > mmax) mmax = sum;
      if (sum < mmin) mmin = sum;
    }
    if (mmax - mmin < best) best = mmax - mmin;
  } while (next_permutation(b, b + k));
  cout << best << endl;
  return 0;
}
