#include <bits/stdc++.h>
using namespace std;
int n, k, a[10][10], p[10];
char second[20];
int main() {
  scanf("%d%d\n", &n, &k);
  for (int i = 0; i < n; ++i) {
    gets(second);
    for (int j = 0; j < k; ++j) a[i][j] = second[j] - '0';
  }
  for (int i = 0; i < k; ++i) p[i] = i;
  int ans = 1000000000;
  do {
    int Min = 1000000000, Max = 0;
    for (int i = 0; i < n; ++i) {
      int x = 0;
      for (int j = 0; j < k; ++j) x = 10 * x + a[i][p[j]];
      Min = min(Min, x);
      Max = max(Max, x);
    }
    ans = min(ans, Max - Min);
  } while (next_permutation(p, p + k));
  printf("%d", ans);
  return 0;
}
