#include <bits/stdc++.h>
using namespace std;
int n, k, seq[9], mini, maxi, aux, res;
bool u[9];
char s[9][9];
void solve(int lvl) {
  if (lvl > k) {
    mini = 0x3f3f3f3f;
    maxi = -mini;
    for (int i = 1; i <= n; i++) {
      aux = 0;
      for (int j = 1; j <= k; j++) aux = aux * 10 + (s[i][seq[j]] - '0');
      mini = min(mini, aux);
      maxi = max(maxi, aux);
    }
    res = min(res, maxi - mini);
  } else {
    for (int d = 1; d <= k; d++) {
      if (u[d]) continue;
      u[d] = true;
      seq[lvl] = d;
      solve(lvl + 1);
      u[d] = false;
    }
  }
}
int main() {
  scanf("%d %d\n", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%s", s[i] + 1);
  }
  memset(u, false, sizeof u);
  memset(seq, -1, sizeof seq);
  res = 0x3f3f3f3f;
  solve(1);
  printf("%d\n", res);
}
