#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  int x;
  cin >> n >> k;
  vector<string> vec;
  vector<int> longs;
  vector<int> permute;
  string s, s1;
  stringstream ss;
  for (int i = 0; i < n; i++) {
    cin >> s;
    vec.push_back(s);
    ss << s;
    ss >> x;
    ss.clear();
    longs.push_back(x);
  }
  sort(longs.begin(), longs.end());
  int Min = longs[n - 1] - longs[0];
  s.clear();
  for (int i = 0; i < k; i++) {
    permute.push_back(i);
  }
  while (next_permutation(permute.begin(), permute.end())) {
    for (int i = 0; i < n; i++) {
      s1 = vec[i];
      for (int j = 0; j < k; j++) {
        s1[j] = vec[i][permute[j]];
      }
      ss << s1;
      ss >> x;
      longs[i] = x;
      ss.clear();
    }
    sort(longs.begin(), longs.end());
    if (Min > longs[n - 1] - longs[0]) {
      Min = longs[n - 1] - longs[0];
    }
  }
  cout << Min;
  return 0;
}
