#include <bits/stdc++.h>
using namespace std;
int min(int a, int b) { return a > b ? b : a; }
int max(int a, int b) { return a > b ? a : b; }
const int INF = 0x7fffffff;
const int Max = 10;
string s[Max];
int flag[Max];
int num[Max];
int main() {
  int n, k;
  int ans;
  int minv;
  int maxv;
  while (cin >> n >> k) {
    ans = INF;
    for (int i = 0; i < k; i++) flag[i] = i;
    for (int i = 0; i < n; i++) cin >> s[i];
    do {
      maxv = -1;
      minv = INF;
      for (int i = 0; i < n; i++) {
        int temp = 0;
        for (int j = 0; j < k; j++) {
          temp = temp * 10 + (s[i][flag[j]] - '0');
        }
        num[i] = temp;
      }
      for (int i = 0; i < n; i++) {
        maxv = max(maxv, num[i]);
        minv = min(minv, num[i]);
      }
      if (maxv - minv < ans) ans = maxv - minv;
    } while (next_permutation(flag, flag + k));
    cout << ans << endl;
  }
  return 0;
}
