#include <bits/stdc++.h>
using namespace std;
const long long INF = (long long)1e12;
int N, K;
string s[8];
vector<int> perms;
long long makeNumber(vector<int> perms, string s) {
  vector<int> pos(K);
  for (int i = 0; i < s.size(); i++) pos[i] = s[i] - '0';
  long long ans = 0LL;
  for (int i = 0; i < perms.size(); i++) ans = ans * 10 + pos[perms[i]];
  return ans;
}
int main() {
  cin >> N >> K;
  for (int i = 0; i < N; i++) cin >> s[i];
  for (int i = 0; i < K; i++) perms.push_back(i);
  long long ans = INF;
  do {
    vector<long long> nums;
    for (int i = 0; i < N; i++) nums.push_back(makeNumber(perms, s[i]));
    sort(nums.begin(), nums.end());
    ans = min(ans, nums.back() - nums[0]);
  } while (next_permutation(perms.begin(), perms.end()));
  cout << ans << endl;
  return 0;
}
