#include <bits/stdc++.h>
using namespace std;
const int MAXN = 8 + 1;
int n, k, perm[MAXN], minn = 1000 * 1000 * 1000, num[MAXN];
string s[MAXN], sp[MAXN];
int power(int a, int b) {
  if (b == 0)
    return 1;
  else
    return a * power(a, b - 1);
}
void CTN(int i) {
  num[i] = 0;
  for (int j = 0; j < k; j++)
    num[i] += int(sp[i][j] - '0') * power(10, k - j - 1);
}
int abs(int n) {
  if (n < 0)
    return -n;
  else
    return n;
}
void find() {
  for (int i = 0; i < n; i++) CTN(i);
  int max1 = 0, min1 = 1000 * 1000 * 1000;
  for (int i = 0; i < n; i++) {
    if (num[i] > max1) max1 = num[i];
    if (num[i] < min1) min1 = num[i];
  }
  if (abs(max1 - min1) < minn) minn = abs(max1 - min1);
}
int main() {
  cin >> n >> k;
  for (int i = 0; i < k; i++) perm[i] = i + 1;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    sp[i] = s[i];
  }
  do {
    for (int i = 0; i < n; i++)
      for (int j = 0; j < k; j++) sp[i][j] = s[i][perm[j] - 1];
    find();
  } while (next_permutation(perm, perm + k));
  cout << minn << endl;
  return 0;
}
