#include <bits/stdc++.h>
using namespace std;
int convert(int A[], int K) {
  int n = 0;
  for (int i = K - 1, f = 1; i >= 0; i--, f *= 10) {
    n += A[i] * f;
  }
  return n;
}
int main() {
  int N, K;
  cin >> N >> K;
  cin.get();
  int A[N][K];
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < K; j++) {
      A[i][j] = cin.get() - '0';
    }
    cin.get();
  }
  int P[K];
  for (int i = 0; i < K; i++) P[i] = i;
  int B[N], pn[K], d, min_d = numeric_limits<int>::max();
  do {
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < K; j++) {
        pn[j] = A[i][P[j]];
      }
      B[i] = convert(pn, K);
    }
    d = *max_element(B, B + N) - *min_element(B, B + N);
    min_d = min(d, min_d);
  } while (next_permutation(P, P + K));
  cout << min_d << endl;
}
