#include <bits/stdc++.h>
using namespace std;
int numbers[10], ans = int(1e9), n, k;
string str[12];
int permut(string t) {
  vector<int> digits;
  for (int i = 0; i < (int)(t).size(); i++) digits.push_back(t[i] - '0');
  int temp = 0;
  for (int i = 0; i < k; i++) {
    temp += digits[numbers[i] - 1];
    if (i != k - 1) temp *= 10;
  }
  return temp;
}
int main() {
  cin >> n >> k;
  for (int i = 1; i <= k; i++) numbers[i - 1] = i;
  for (int i = 0; i < n; i++) cin >> str[i];
  do {
    vector<int> num;
    for (int i = 0; i < n; i++) num.push_back(permut(str[i]));
    sort(num.begin(), num.end());
    ans = min(ans, num.back() - num[0]);
    num.clear();
  } while (next_permutation(numbers, numbers + k));
  cout << ans;
  return 0;
}
