#include <bits/stdc++.h>
using namespace std;
char input[10000][10000];
int permutation[10000], number[10000];
int main(int argc, char **argv) {
  int n, k;
  cin >> n >> k;
  int diff = INT_MAX;
  for (int i = 0; i < n; i++) {
    cin >> input[i];
  }
  for (int i = 0; i < k; i++) {
    permutation[i] = i;
  }
  do {
    memset(number, 0, sizeof(number));
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < k; j++) {
        number[i] = number[i] * 10 + (input[i][permutation[j]] - '0');
      }
    }
    diff = min(diff, *max_element(number, number + n) -
                         *min_element(number, number + n));
  } while (next_permutation(permutation, permutation + k));
  cout << diff << endl;
}
