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