#include <bits/stdc++.h>
using namespace std;
vector<int> pos;
int g(string& x) { return atoi(x.c_str()); }
int main() {
  int n, k;
  cin >> n >> k;
  vector<string> nums(n);
  for (int i = 0; i < n; ++i) {
    cin >> nums[i];
  }
  pos.resize(k);
  for (int i = 0; i < k; ++i) pos[i] = i;
  int d = numeric_limits<int>().max();
  do {
    vector<string> local(n, string(k + 1, '\0'));
    for (int j = 0; j < n; ++j) {
      for (unsigned i = 0; i < pos.size(); ++i) local[j][i] = nums[j][pos[i]];
    }
    vector<int> nums(n);
    transform(local.begin(), local.end(), nums.begin(), g);
    int mi = *min_element(nums.begin(), nums.end());
    int ma = *max_element(nums.begin(), nums.end());
    if (d > ma - mi) d = ma - mi;
  } while (next_permutation(pos.begin(), pos.end()));
  cout << d << endl;
}
