#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  string s[10];
  int vect[10];
  int min, max, num, ans;
  while (cin >> n >> k) {
    getline(cin, s[0]);
    for (int i = 0; i < n; i++) {
      getline(cin, s[i]);
    }
    for (int i = 0; i < k; i++) {
      vect[i] = i;
    }
    ans = 100000000;
    do {
      min = 100000000;
      max = -1;
      for (int i = 0; i < n; i++) {
        num = 0;
        for (int j = 0; j < k; j++) {
          num = num * 10 + s[i][vect[j]] - '0';
        }
        if (num < min) min = num;
        if (num > max) max = num;
      }
      if (ans > max - min) ans = max - min;
    } while (next_permutation(vect, vect + k));
    cout << ans << endl;
  }
}
