#include <bits/stdc++.h>
using namespace std;
const int N = 10;
int toInt(int p[], char *str, int b) {
  int sum = 0;
  for (int i = 0; i < b; i++) {
    sum = sum * 10 + str[p[i]] - '0';
  }
  return sum;
}
char Int[N][N];
int p[N];
int temp[N];
int main() {
  int n, k;
  while (cin >> n >> k) {
    getchar();
    for (int i = 0; i < n; i++) {
      cin >> Int[i];
      getchar();
    }
    for (int i = 0; i < N; i++) p[i] = i;
    int min = ~0u >> 1;
    for (int i = 0; i < n; i++) {
      temp[i] = toInt(p, Int[i], k);
    }
    sort(temp, temp + n);
    if ((temp[n - 1] - temp[0]) < min) min = temp[n - 1] - temp[0];
    while (next_permutation(p, p + k)) {
      for (int i = 0; i < n; i++) {
        temp[i] = toInt(p, Int[i], k);
      }
      sort(temp, temp + n);
      if ((temp[n - 1] - temp[0]) < min) min = temp[n - 1] - temp[0];
    }
    cout << min << endl;
  }
  return 0;
}
