#include <bits/stdc++.h>
using namespace std;
string A[15];
long long nr[100];
int N, K;
long long mn = 1000000000;
int main() {
  scanf("%d %d\n", &N, &K);
  for (int i = 1; i <= N; i++) cin >> A[i];
  vector<int> v;
  for (int i = 1; i <= K; i++) v.push_back(i);
  do {
    for (int i = 1; i <= N; i++) {
      nr[i] = 0;
      for (int j = 0; j < K; j++) nr[i] = nr[i] * 10 + (A[i][v[j] - 1] - '0');
    }
    sort(nr + 1, nr + N + 1);
    mn = min(mn, nr[N] - nr[1]);
  } while (next_permutation(v.begin(), v.end()));
  cout << mn;
  return 0;
}
