#include <bits/stdc++.h>
using namespace std;
int n, k, a[10], p[10], difmin = 1000000000;
bool v[10];
int rear(int t) {
  int i, r = 0, rr[10], vv[10], nr = k;
  for (i = 0; i < 10; ++i) rr[i] = vv[i] = 0;
  while (t) {
    vv[nr--] = t % 10;
    t /= 10;
  }
  for (i = 1; i <= k; ++i) {
    rr[i] = vv[p[i]];
    r = r * 10 + rr[i];
  }
  return r;
}
void ver() {
  int smin = 1000000000, smax = 0;
  for (int i = 1; i <= n; ++i) {
    int t = rear(a[i]);
    smin = min(smin, t);
    smax = max(smax, t);
  }
  difmin = min(difmin, smax - smin);
}
void back(int pa) {
  int i;
  if (pa == k + 1) {
    ver();
    return;
  }
  for (i = 1; i <= k; ++i)
    if (!v[i]) {
      v[i] = 1;
      p[pa] = i;
      back(pa + 1);
      v[i] = 0;
    }
}
int main() {
  int i;
  cin >> n >> k;
  int sss = 0;
  for (i = 1; i <= n; ++i) cin >> a[i], sss = max(sss, a[i]);
  back(1);
  cout << difmin;
  return 0;
}
