#include <bits/stdc++.h>
using namespace std;
int getnum(const string& s, char arr[], int k) {
  int ans = 0;
  for (int i = 0; i < k; ++i) {
    ans *= 10;
    ans += s[arr[i]];
  }
  return ans;
}
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  char arr[100];
  string sarr[10];
  for (int i = 0; i < n; ++i) {
    scanf("%s", arr);
    sarr[i] = arr;
  }
  for (int i = 0; i < k; ++i) {
    arr[i] = i;
  }
  int fact = 1;
  for (int i = 2; i <= k; ++i) fact *= i;
  int ans = 1000000000;
  for (int i = 0; i < fact; ++i) {
    int M = 0;
    int m = 1000000000;
    for (int i = 0; i < n; ++i) {
      int a = getnum(sarr[i], arr, k);
      if (a > M) M = a;
      if (a < m) m = a;
    }
    if (M - m < ans) ans = M - m;
    next_permutation(arr, arr + k);
  }
  printf("%d\n", ans);
}
