#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  int mx = 0, mn = 1e9;
  vector<string> v(n);
  for (int i = 0; i < n; i++) cin >> v[i];
  int ans = 1e9;
  int ma;
  int arr[8];
  for (int i = 0; i < m; i++) arr[i] = i;
  do {
    int mx = 0, mn = 1e9;
    for (int k = 0; k < n; k++) {
      string s;
      for (int j = 0; j < m; j++) s += v[k][arr[j]];
      int d = stoi(s);
      mn = min(mn, d);
      mx = max(mx, d);
    }
    ans = min(ans, mx - mn);
  } while (next_permutation(arr, arr + m));
  cout << ans << "\n";
}
