#include <bits/stdc++.h>
using namespace std;
int n, k;
char c[10][10];
int use[10];
int r[10];
int ans;
int calc(int x) {
  int t = 0;
  for (int i = 0; i < k; i++) {
    t = t * 10 + c[x][r[i]] - '0';
  }
  return t;
}
void dfs(int lv) {
  if (lv == k) {
    int mx = -1, mn = 1000000000;
    for (int i = 0; i < n; i++) {
      int t = calc(i);
      mx = max(mx, t);
      mn = min(mn, t);
    }
    ans = min(ans, mx - mn);
    return;
  }
  for (int i = 0; i < k; i++) {
    if (use[i]) continue;
    use[i] = 1;
    r[lv] = i;
    dfs(lv + 1);
    use[i] = 0;
  }
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n; i++) {
    scanf("%s", c[i]);
  }
  memset(use, 0, sizeof(use));
  memset(r, 0, sizeof(r));
  ans = 1000000000;
  dfs(0);
  printf("%d\n", ans);
  return 0;
}
