#include <bits/stdc++.h>
using namespace std;
__inline int permutate(int num, vector<int> permutation, int k) {
  int res = 0;
  for (int i = (int)(0), _m = (int)(permutation.size()); i < _m; ++i)
    res = (res * 10) + ((num / (int)powl(10, k - permutation[i])) % 10);
  return res;
}
int main() {
  int n, k, tmp, mn, mx, difference;
  vector<int> numbers, allPermutations;
  while (cin >> n >> k) {
    numbers.clear();
    allPermutations.clear();
    for (int i = (int)(0), _m = (int)(n); i < _m; ++i) {
      cin >> tmp;
      numbers.push_back(tmp);
    }
    for (int i = (int)(1), _m = (int)(k + 1); i < _m; ++i)
      allPermutations.push_back(i);
    difference = 2000000000;
    do {
      mn = 2000000000;
      mx = -2000000000;
      for (int i = (int)(0), _m = (int)(n); i < _m; ++i) {
        tmp = permutate(numbers[i], allPermutations, k);
        mn = min(mn, tmp);
        mx = max(mx, tmp);
      }
      difference = min(difference, mx - mn);
    } while (next_permutation(allPermutations.begin(), allPermutations.end()));
    cout << difference << endl;
  }
}
