#include <bits/stdc++.h>
using namespace std;
int d[10] = {1,      10,      100,      1000,      10000,
             100000, 1000000, 10000000, 100000000, 1000000000};
int main() {
  int n, k, a, maxx, minn, i, j, res;
  string s[10];
  while (cin >> n >> k) {
    for (i = 0; i < n; ++i) {
      cin >> s[i];
    }
    vector<int> v;
    for (i = 0; i < k; ++i) v.push_back(i);
    res = 100000000;
    do {
      minn = 100000000;
      maxx = 0;
      for (i = 0; i < n; ++i) {
        a = 0;
        for (j = 0; j < k; ++j) {
          a += d[j] * (s[i][v[j]] - '0');
        }
        if (maxx < a) maxx = a;
        if (minn > a) minn = a;
      }
      if (maxx - minn < res) res = maxx - minn;
    } while (next_permutation(v.begin(), v.end()));
    printf("%d\n", res);
  }
  return 0;
}
