#include <bits/stdc++.h>
using namespace std;
template <typename T>
void print(const T& data) {
  cout << data << endl;
}
int gcd(int x, int y) {
  while (y > 0) {
    int tmp = y;
    y = x % y;
    x = tmp;
  }
  return x;
}
void rearrange(vector<int>& digits, vector<int>& vec) {
  vector<int> dd(vec.size());
  for (int& digit : digits) {
    for (size_t i = 0; i < vec.size(); ++i) {
      dd[i] = digit % 10;
      digit /= 10;
    }
    digit = 0;
    for (size_t i = 0; i < vec.size(); ++i) {
      digit = digit * 10 + dd[vec[i]];
    }
  }
}
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> digits(n);
  for (int i = 0; i < n; ++i) cin >> digits[i];
  if (n == 1) {
    cout << 0 << endl;
    return 0;
  }
  vector<int> vec(k);
  for (int i = 0; i < k; ++i) vec[i] = i;
  int min_value = 99999999;
  do {
    vector<int> tmp(digits);
    rearrange(tmp, vec);
    sort(tmp.begin(), tmp.end());
    min_value = min(min_value, tmp[n - 1] - tmp[0]);
  } while (next_permutation(vec.begin(), vec.end()));
  cout << min_value << endl;
  return 0;
}
