#include <bits/stdc++.h>
long m, n, arr[9];
char matRes[9][9];
char mat[9][9];
long res = 10000000000, max, min, b, maxi, mini, a;
using namespace std;
int main() {
  scanf("%ld%ld", &m, &n);
  for (a = 0; a < m; a++) scanf("%s", mat[a]);
  for (a = 1; a <= n; a++) {
    arr[a - 1] = a;
  }
  maxi = -1;
  mini = 100000000;
  for (b = 0; b < m; b++) {
    if (atoi(mat[b]) > maxi) maxi = atoi(mat[b]);
    if (atoi(mat[b]) < mini) mini = atoi(mat[b]);
  }
  if (res > maxi - mini) res = maxi - mini;
  while (next_permutation(arr, arr + n)) {
    maxi = -1;
    mini = 100000000;
    for (a = 0; a < n; a++) {
      for (b = 0; b < m; b++) {
        matRes[b][a] = mat[b][arr[a] - 1];
      }
    }
    for (b = 0; b < m; b++) {
      if (atoi(matRes[b]) > maxi) maxi = atoi(matRes[b]);
      if (atoi(matRes[b]) < mini) mini = atoi(matRes[b]);
    }
    if (res > maxi - mini) res = maxi - mini;
  }
  printf("%ld\n", res);
}
