#include <bits/stdc++.h>
using namespace std;
int stoInt(string s) {
  int len = s.size();
  int num = 0;
  for (int i = 0; i < len; i++) {
    num = (num * 10) + (s[i] - 48);
  }
  return num;
}
int main() {
  int n, k, i = 0;
  cin >> n >> k;
  int currentAns = INT_MAX;
  vector<string> store;
  vector<int> storeI;
  while (i++ < n) {
    string s;
    cin >> s;
    int x = stoInt(s);
    storeI.push_back(x);
    store.push_back(s);
  }
  string permutationOrder = "";
  for (int i = 0; i < k; i++) {
    string temp(1, (48 + i));
    permutationOrder = permutationOrder + temp;
  }
  sort(storeI.begin(), storeI.end());
  currentAns = (storeI[n - 1] - storeI[0]);
  while (next_permutation(permutationOrder.begin(), permutationOrder.end())) {
    vector<int> newStoreI;
    string permutation = permutationOrder;
    for (int i = 0; i < n; i++) {
      string curr = store[i];
      int temp = 0;
      for (int i = 0; i < permutation.size(); i++) {
        char currentChar = curr[permutation[i] - '0'];
        int val = currentChar - '0';
        temp = temp * 10 + val;
      }
      newStoreI.push_back(temp);
    }
    sort(newStoreI.begin(), newStoreI.end());
    currentAns = min(currentAns, (newStoreI[n - 1] - newStoreI[0]));
  }
  cout << currentAns;
  return 0;
}
