#include <bits/stdc++.h>
using namespace std;
int j = 0;
int fact(int i) {
  if (i == 0)
    return 1;
  else
    return fact(i - 1) * i;
}
string perest(string str) {
  char temp;
  string str2 = str;
  if (j != 0) {
    if (str.length() == 0)
      return "";
    else if (str.length() == 1)
      return str;
    else {
      int t = (j / fact(str2.length() - 1)) % str2.length();
      temp = str.at(t);
      str.at(t) = str.at(0);
      str.at(0) = temp;
      return str.substr(0, 1) + perest(str.substr(1, str.length() - 1));
    }
  }
  return str;
}
int main() {
  int max, min;
  int razn = 9999999999;
  vector<string> nums;
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    string t;
    cin >> t;
    nums.push_back(t);
  }
  k = fact(k);
  if (n == 1) {
    cout << 0;
    return 0;
  }
  for (j = 0; j < k; j++) {
    max = -999999999;
    min = +999999999;
    for (int i = 0; i < nums.size(); i++) {
      int temp = atoi(perest(nums.at(i)).c_str());
      if (temp > max) max = temp;
      if (temp < min) min = temp;
    }
    if (max - min < razn && max != min) {
      razn = max - min;
    }
  }
  cout << razn;
  return 0;
}
