#include <bits/stdc++.h>
using namespace std;
int main() {
  long long x, y, ans = 99999999999, value, mx, mn;
  cin >> x >> y;
  string str[10];
  long long aray[10];
  for (int i = 0; i < x; i++) {
    cin >> str[i];
  }
  for (int i = 0; i < y; i++) {
    aray[i] = i;
  }
  do {
    mn = 99999999999;
    mx = -99999999999;
    for (int i = 0; i < x; i++) {
      value = 0;
      for (int j = 0; j < y; j++) {
        value = value * 10 + (str[i][aray[j]] - '0');
      }
      mx = max(mx, value);
      mn = min(mn, value);
    }
    ans = min(ans, mx - mn);
  } while (next_permutation(aray, aray + y));
  cout << ans << endl;
  return 0;
}
