#include <bits/stdc++.h>
using namespace std;
string s[11];
int a[11];
int n, k;
int ans = 0x7fffffff;
string t[11];
int num[11];
int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
  }
  for (int i = 0; i < k; i++) {
    a[i] = i;
  }
  do {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < k; j++) {
        t[i][j] = s[i][a[j]];
      }
    }
    for (int i = 0; i < n; i++) {
      int tt = 0;
      for (int j = 0; j < k; j++) {
        tt = tt * 10 + t[i][j] - '0';
      }
      num[i] = tt;
    }
    sort(num, num + n);
    ans = min(ans, num[n - 1] - num[0]);
  } while (next_permutation(a, a + k));
  cout << ans << endl;
  return 0;
}
