#include <bits/stdc++.h>
using namespace std;
int n, k;
char nums[10][10];
int mini = 1000000000;
int eval(vector<int> x) {
  int cnv[10];
  for (int i = 0; i < n; i++) {
    cnv[i] = 0;
    for (int j = 0; j < k; j++) {
      cnv[i] = cnv[i] * 10 + (nums[i][x[j]] - '0');
    }
  }
  sort(cnv, cnv + n);
  return (cnv[n - 1] - cnv[0]);
}
void BF(int a, vector<int> cur) {
  if (a == k) {
    int curMin = eval(cur);
    mini = min(curMin, mini);
    return;
  }
  for (int i = 0; i < k; i++) {
    bool can = 1;
    for (int j = 0; j < a; j++) {
      if (cur[j] == i) {
        can = 0;
        break;
      }
    }
    if (can) {
      vector<int> newCur = cur;
      newCur.push_back(i);
      BF(a + 1, newCur);
    }
  }
}
int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> nums[i];
  }
  vector<int> temp;
  temp.clear();
  BF(0, temp);
  cout << mini << '\n';
}
