#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k, i, j, min, max, num, ans = 1000000000;
  int a[8] = {0, 1, 2, 3, 4, 5, 6, 7};
  char s[8][10];
  cin >> n >> k;
  for (i = 0; i < n; i++) cin >> s[i];
  do {
    min = 1000000000;
    max = 0;
    for (i = 0; i < n; i++) {
      num = 0;
      for (j = 0; j < k; j++) num = num * 10 + (s[i][a[j]] - '0');
      if (min > num) min = num;
      if (max < num) max = num;
    }
    if (max - min < ans) ans = max - min;
  } while (next_permutation(a, a + k));
  cout << ans << endl;
}
