#include <bits/stdc++.h>
using namespace std;
int fromStrtoInt(string s, string c) {
  int x = 0;
  for (int i = 0; i < c.size(); i++) {
    x *= 10;
    x += s[c[i] - '0'] - '0';
  }
  return x;
}
int main() {
  int n, k;
  cin >> n >> k;
  string s, arr[n];
  for (int i = 0; i < n; i++) cin >> arr[i];
  for (int i = 0; i < k; i++) s += i + '0';
  int a[n];
  int ans = 1e9;
  do {
    for (int i = 0; i < n; i++) a[i] = fromStrtoInt(arr[i], s);
    sort(a, a + n);
    ans = min(ans, a[n - 1] - a[0]);
  } while (next_permutation(s.begin(), s.end()));
  cout << ans;
  return 0;
}
