#include <bits/stdc++.h>
using namespace std;
char s[10][10];
int ans[10], flag[10], c[10];
int res, n, k;
void dfs(int m) {
  if (m == k) {
    for (int i = 1; i <= n; i++) {
      ans[i] = 0;
      for (int j = 0; j < k; j++) {
        ans[i] = ans[i] * 10 + (s[i][c[j]] - '0');
      }
    }
    sort(ans + 1, ans + n + 1);
    if (res > ans[n] - ans[1]) {
      res = ans[n] - ans[1];
    }
    return;
  }
  for (int i = 0; i < k; i++) {
    if (flag[i] == 0) {
      c[m] = i;
      flag[i] = 1;
      dfs(m + 1);
      flag[i] = 0;
    }
  }
  return;
}
int main() {
  res = 1000000000;
  scanf("%d%d", &n, &k);
  getchar();
  for (int i = 1; i <= n; i++) {
    scanf("%s", s[i]);
  }
  dfs(0);
  printf("%d\n", res);
  return 0;
}
