#include <bits/stdc++.h>
using namespace std;
char s[8][9];
int a[8];
int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) cin >> s[i];
  for (int i = 0; i < k; i++) a[i] = i;
  char tmp[9];
  tmp[8] = '\0';
  int mm = 1e9;
  do {
    int mx = 0;
    int mn = 1e9;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < k; j++) {
        tmp[a[j]] = s[i][j];
      }
      mn = min<int>(mn, atoi(tmp));
      mx = max<int>(mx, atoi(tmp));
    }
    mm = min<int>(mm, mx - mn);
  } while (next_permutation(a, a + k));
  cout << mm << endl;
  return 0;
}
