#include <bits/stdc++.h>
using namespace std;
int perm[10], m, n, minim = 100000000;
char cifre[400005][8];
bool cifraFolosita[8];
void Calculare_Minim() {
  int i, j, nr, max1 = -1, min1 = 100000000;
  for (i = 0; i < n; ++i) {
    nr = 0;
    for (j = 0; j < m; ++j) nr = nr * 10 + cifre[i][perm[j]] - '0';
    if (max1 < nr) max1 = nr;
    if (min1 > nr) min1 = nr;
  }
  if (minim > max1 - min1) minim = max1 - min1;
}
void Aflare_Permutari(int k) {
  if (m == k)
    Calculare_Minim();
  else {
    for (int i = 0; i < m; ++i)
      if (!cifraFolosita[i]) {
        cifraFolosita[i] = 1;
        perm[k] = i;
        Aflare_Permutari(k + 1);
        cifraFolosita[i] = 0;
      }
  }
}
void Solve() {
  int i;
  cin >> n >> m;
  cin.get();
  for (i = 0; i < n; ++i) cin.getline(cifre[i], m + 1);
  Aflare_Permutari(0);
  cout << minim;
}
int main() {
  Solve();
  return 0;
}
