#include <bits/stdc++.h>
using namespace std;
long long arr[8][9];
long long get_max(long long perm[], long long k, long long n) {
  vector<long long> temp;
  for (long long i = 0; i < n; i++) {
    long long temp1 = 0;
    for (long long j = 0; j < k; j++)
      temp1 += arr[i][perm[j]] * pow((long long)10, k - 1 - j);
    temp.push_back(temp1);
  }
  sort(temp.begin(), temp.end());
  long long mini = temp[0];
  long long maxi = temp[n - 1];
  return maxi - mini;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long n, k;
  cin >> n >> k;
  memset(arr, 0, sizeof(arr));
  for (long long i = 0; i < n; i++) {
    for (long long j = 1; j <= k; j++) {
      char c;
      cin >> c;
      long long temp = c - 48;
      arr[i][j] = temp;
    }
  }
  long long perm[k];
  for (long long i = 0; i < k; i++) perm[i] = i + 1;
  long long ans = 9999999999;
  do {
    ans = min(ans, get_max(perm, k, n));
  } while (next_permutation(perm, perm + k));
  cout << ans << "\n";
  return 0;
}
