#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, k;
  cin >> n >> k;
  vector<int> arr(k);
  for (int i = 0; i < k; i++) {
    arr[i] = i + 1;
  }
  vector<string> allPos;
  do {
    string s = "";
    for (int x : arr) {
      s += (x + '0');
    }
    allPos.emplace_back(s);
  } while (next_permutation((arr).begin(), (arr).end()));
  vector<string> v(n), tempV(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
    tempV[i] = v[i];
  }
  int ans = INT_MAX;
  for (string current : allPos) {
    vector<int> temp(n);
    int index = 0;
    for (char ch : current) {
      for (int i = 0; i < n; i++) {
        tempV[i][index] = v[i][ch - '0' - 1];
      }
      index++;
    }
    for (int i = 0; i < n; i++) {
      temp[i] = stoi(tempV[i]);
    }
    ans = min(ans, *max_element((temp).begin(), (temp).end()) -
                       *min_element((temp).begin(), (temp).end()));
  }
  cout << ans << "\n";
}
