#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, k;
  cin >> n >> k;
  string s[n];
  long long arr[k];
  long long p10[k];
  p10[0] = 1;
  for (int i = 0; i < n; i++) cin >> s[i];
  long long ans = 999999999;
  for (int i = 0; i < k; i++) {
    arr[i] = i;
    if (i) p10[i] = 10 * p10[i - 1];
  }
  do {
    vector<long long> num;
    for (int i = 0; i < n; i++) {
      int temp = 0;
      for (int j = 0; j < k; j++) temp += p10[j] * (s[i][arr[j]]);
      num.push_back(temp);
    }
    sort(num.begin(), num.end());
    ans = min(num[num.size() - 1] - num[0], ans);
  } while (next_permutation(arr, arr + k));
  cout << ans << endl;
}
