#include <bits/stdc++.h>
using namespace std;
int n, a, b;
int k, i, j, x, s, q;
char c[9];
int mii = 100000000;
vector<vector<int> > digit(9, vector<int>());
vector<vector<int> > res(9, vector<int>());
vector<int> mask(9);
int sum(vector<vector<int> > arr) {
  int mmin = 100000000;
  int mmax = 0;
  int x, i, j, q;
  for (i = 0; i < n; i++) {
    x = 0;
    for (j = k - 1, q = 1; j >= 0; j--, q *= 10) {
      x += q * arr[i][j];
    }
    if (x < mmin) mmin = x;
    if (x > mmax) mmax = x;
  }
  return mmax - mmin;
}
void permute(vector<vector<int> > arr, vector<int> mask, int x) {
  int l;
  for (int j = 0; j < k; j++) {
    if (!mask[j]) {
      mask[j] = 1;
      for (int q = 0; q < n; q++) {
        arr[q][x] = digit[q][j];
      }
      if (x == k - 1) {
        l = sum(arr);
        if (l < mii) mii = l;
      } else {
        permute(arr, mask, x + 1);
      }
      mask[j] = 0;
    }
  }
}
int main() {
  scanf("%d %d", &n, &k);
  for (int i = 0; i < n; ++i) {
    scanf("%s", &c);
    for (j = 0; j < k; ++j) {
      digit[i].push_back(c[j] - '0');
    }
  }
  permute(digit, mask, 0);
  printf("%d\n", mii);
  return 0;
}
