#include <bits/stdc++.h>
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  {
    long long int n, k, i, m, mx, d = 4e18, f, num, j;
    cin >> n >> k;
    long long int a[k], b[n];
    for (i = 0; i < k; i++) a[i] = i;
    string s[n];
    for (i = 0; i < n; i++) cin >> s[i];
    do {
      m = 4e18;
      mx = 0;
      f = 0;
      for (i = 0; i < n; i++) {
        num = 0;
        for (j = 0; j < k; j++) {
          num *= 10;
          num += (s[i][a[j]] - '0');
        }
        mx = max(mx, num);
        m = min(m, num);
      }
      d = min(d, mx - m);
    } while (next_permutation(a, a + k));
    cout << d;
    cout << '\n';
  }
  return 0;
}
