#include <bits/stdc++.h>
char s;
int st[1005];
int a[30][10];
int i, j, t, f, max, x[10], n, m, k, min, l;
int h[1005], hj[1005];
int d[10][10];
int g[10];
int max1 = 99999999;
void permutation(int k, int m) {
  int i, j;
  if (k == m) {
    memset(x, 0, sizeof(x));
    for (f = 0; f < n; f++) {
      for (l = m; l >= 0; l--) {
        x[f] += a[f][l] * g[m - l];
      }
    }
    max = 0;
    min = 99999999;
    for (f = 0; f < n; f++) {
      if (min > x[f]) {
        min = x[f];
      }
      if (max < x[f]) {
        max = x[f];
      }
    }
    if (max1 > (max - min)) {
      max1 = max - min;
    }
  } else {
    for (j = k; j <= m; j++) {
      for (i = 0; i < n; i++) {
        if (k == j) continue;
        a[i][k] = a[i][k] ^ a[i][j];
        a[i][j] = a[i][j] ^ a[i][k];
        a[i][k] = a[i][k] ^ a[i][j];
      }
      permutation(k + 1, m);
      for (i = 0; i < n; i++) {
        if (k == j) continue;
        a[i][k] = a[i][k] ^ a[i][j];
        a[i][j] = a[i][j] ^ a[i][k];
        a[i][k] = a[i][k] ^ a[i][j];
      }
    }
  }
}
int main() {
  g[0] = 1;
  for (i = 1; i < 9; i++) {
    g[i] = g[i - 1] * 10;
  }
  memset(d, 0, sizeof(d));
  scanf("%d %d%*c", &n, &m);
  memset(a, 0, sizeof(s));
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      scanf("%c", &s);
      a[i][j] = s - '0';
    }
    getchar();
  }
  permutation(0, m - 1);
  printf("%d\n", max1);
  return 0;
}
