#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 7, M = 1e9, OO = 0x3f3f3f3f;
const long long int MOD = 1000000007;
int main() {
  int n, k;
  cin >> n >> k;
  vector<string> v;
  string s;
  for (int i = 0; i < int(n); ++i) {
    cin >> s;
    v.push_back(s);
  }
  vector<int> arrange;
  int ans = M;
  for (int i = 0; i < int(k); ++i) arrange.push_back(i);
  do {
    int Max = 0, Min = M, num;
    for (int i = 0; i < int(n); ++i) {
      s = v[i];
      num = 0;
      for (int j = 0; j < int(k); ++j) {
        num += s[arrange[j]] - '0';
        num *= 10;
      }
      Max = max(Max, num);
      Min = min(Min, num);
    }
    ans = min(ans, Max - Min);
  } while (next_permutation(arrange.begin(), arrange.end()));
  cout << ans / 10;
  return 0;
}
