#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  vector<vector<int>> arr;
  int p[k];
  for (int i = 0; i < k; i++) p[i] = i;
  for (int i = 0; i < n; i++) {
    int num;
    cin >> num;
    vector<int> v;
    int j = 0;
    while (j < k) {
      v.push_back(num % 10);
      num = num / 10;
      j++;
    }
    reverse(v.begin(), v.end());
    arr.push_back(v);
  }
  int ans = INT_MAX;
  do {
    int mn = INT_MAX, mx = INT_MIN;
    for (int i = 0; i < n; i++) {
      int num = 0;
      int l = 1;
      for (int j = k - 1; j >= 0; j--) {
        num = num + arr[i][p[j]] * l;
        l *= 10;
      }
      mn = min(mn, num);
      mx = max(mx, num);
    }
    ans = min(ans, mx - mn);
  } while (next_permutation(p, p + k));
  cout << ans << endl;
}
