#include <bits/stdc++.h>
using namespace std;
int n, k;
int func(int a) {
  int rtn = 1;
  for (int i = (2); i < (a + 1); i++) {
    rtn *= i;
  }
  return rtn;
}
string next_perm(string s, vector<int> p) {
  string rtn = "";
  for (int i = (0); i < (p.size()); i++) {
    rtn += s[p[i]];
  }
  return rtn;
}
void solve() {
  cin >> n >> k;
  vector<string> nums, nums_org;
  string tmp;
  vector<int> perm;
  for (int i = (0); i < (k); i++) {
    perm.push_back(i);
  }
  for (int i = (0); i < (n); i++) {
    cin >> tmp;
    nums.push_back(tmp);
  }
  copy(nums.begin(), nums.end(), back_inserter(nums_org));
  int nums_int[n];
  int ans = numeric_limits<int>::max();
  int *max_num, *min_num;
  int kex = func(k);
  for (int i = (0); i < (kex + 1); i++) {
    for (int j = (0); j < (n); j++) {
      nums_int[j] = atoi(nums[j].c_str());
    }
    max_num = max_element(nums_int, nums_int + n);
    min_num = min_element(nums_int, nums_int + n);
    ans = min(ans, *max_num - *min_num);
    next_permutation(perm.begin(), perm.end());
    for (int j = (0); j < (n); j++) {
      nums[j] = next_perm(nums_org[j], perm);
    }
  }
  cout << ans << endl;
}
int main(int argc, char *argv[]) {
  solve();
  return 0;
}
