#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> arr(k);
  vector<string> nums(n);
  for (int i = 0; i < n; i++) {
    cin >> nums[i];
  }
  for (int i = 0; i < k; i++) {
    arr[i] = i;
  }
  int ans = INT_MAX;
  do {
    int mn = INT_MAX;
    int mx = 0;
    for (int i = 0; i < n; i++) {
      string ns = "";
      for (int j = 0; j < k; j++) {
        ns += nums[i][arr[j]];
      }
      int num = stoi(ns);
      mn = min(mn, num);
      mx = max(mx, num);
    }
    ans = min(ans, mx - mn);
  } while (next_permutation(arr.begin(), arr.end()));
  cout << ans << endl;
}
