#include <bits/stdc++.h>
using namespace std;
int pow(int b, int e) {
  int count = 1;
  for (int i = 0; i < e; i++) {
    count *= b;
  }
  return count;
}
int arrToNum(int *arr, int len) {
  int num = 0;
  for (int p = len - 1, i = 0; p >= 0; p--, i++) {
    num += pow(10, p) * arr[i];
  }
  return num;
}
int main() {
  int n, k;
  cin >> n >> k;
  int *vals = new int[n * k];
  int diff = 2147483647;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < k; j++) {
      char c;
      cin >> c;
      c -= 48;
      vals[i * k + j] = c;
    }
  }
  int *sw = new int[k];
  int *nv = new int[n * k];
  for (int i = 0; i < k; i++) {
    sw[i] = i;
  }
  int a = 0;
  do {
    int max = -2147483648;
    int min = 2147483647;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < k; j++) {
        nv[i * k + j] = vals[i * k + sw[j]];
      }
      int num = arrToNum(nv + i * k, k);
      max = num > max ? num : max;
      min = num < min ? num : min;
    }
    if (max - min < diff) {
      diff = max - min;
    }
    a++;
  } while (next_permutation(sw, sw + k));
  cout << diff << endl;
  return 0;
}
