#include <bits/stdc++.h>
using namespace std;
int in[9];
int n, k;
int a[9];
string s[9];
int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) cin >> s[i];
  for (int i = 0; i < k; i++) in[i] = i;
  int m = 100000000000;
  do {
    int m1 = 0, m2 = 100000000;
    for (int j = 0; j < n; j++) {
      int a = 0;
      for (int l = 0; l < k; l++) a = a * 10 + s[j][in[l]] - '0';
      m1 = max(m1, a);
      m2 = min(m2, a);
    }
    m = min(m, m1 - m2);
  } while (next_permutation(in, in + k));
  cout << m;
  return 0;
}
