#include <bits/stdc++.h>
using namespace std;
int minimum(int n, int k);
char numbers[8][8];
int num[8];
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < k; j++) {
      num[j] = j;
      scanf(" %c", &numbers[i][j]);
      numbers[i][j] -= '0';
    }
  }
  int res = minimum(n, k);
  printf("%d\n", res);
  return 0;
}
int minimum(int n, int k) {
  int m = 0x7fffffff;
  int aux[8];
  int ma, mi;
  do {
    for (int i = 0; i < n; i++) {
      aux[i] = numbers[i][num[0]];
      int pot = 10;
      for (int j = 1; j < k; j++) {
        aux[i] += (numbers[i][num[j]] * pot);
        pot *= 10;
      }
      if (!i)
        ma = mi = aux[i];
      else if (aux[i] > ma)
        ma = aux[i];
      else if (aux[i] < mi)
        mi = aux[i];
    }
    m = min(m, ma - mi);
  } while (next_permutation(num, num + k));
  return m;
}
