#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int N, K;
  cin >> N >> K;
  vector<string> a(N);
  for (int i = 0; i < N; ++i) cin >> a[i];
  vector<int> permutations(K);
  iota(permutations.begin(), permutations.end(), 0);
  int ans = INFINITY;
  do {
    vector<int> new_a(N);
    for (int i = 0; i < N; ++i) {
      string curr = "";
      for (int j = 0; j < K; ++j) {
        curr += a[i][permutations[j]];
      }
      new_a[i] = stoi(curr);
    }
    int mn = *min_element(new_a.begin(), new_a.end());
    int mx = *max_element(new_a.begin(), new_a.end());
    ans = min(ans, mx - mn);
  } while (next_permutation(permutations.begin(), permutations.end()));
  cout << ans << '\n';
  return 0;
}
