#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int n, k;
vector<bool> used;
vector<int> cur;
vector<string> a;
vector<string> toc;
int ans = 1000000000;
void f(int cur_n) {
  for (int i = 0; i < k; i++) {
    if (used[i] == false) {
      used[i] = true;
      cur.push_back(i);
      if (cur_n == k - 1) {
        int mx = -1;
        int mn = 1000000000;
        for (int j = 0; j < n; j++) {
          int p = 0;
          for (auto c : cur) {
            toc[j][c] = a[j][p];
            p++;
          }
          mx = max(mx, atoi(toc[j].c_str()));
          mn = min(mn, atoi(toc[j].c_str()));
        }
        ans = min(ans, mx - mn);
      } else {
        f(cur_n + 1);
      }
      used[i] = false;
      cur.pop_back();
    }
  }
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  srand(time(0));
  a.resize(n);
  cin >> n >> k;
  a.resize(n);
  toc.resize(n, string(k, '0'));
  used.resize(k, false);
  for (int i = 0; i < n; i++) cin >> a[i];
  f(0);
  cout << ans;
  return 0;
}
