#include <bits/stdc++.h>
using namespace std;
const int inf = 100000000;
int n, k;
string str[100];
int flag[10];
bool visit[10];
int ans;
int getans() {
  int mmin = inf;
  int mmax = -1;
  for (int i = 0; i < n; i++) {
    int s = 0;
    for (int j = 0; j < k; j++) {
      s = s * 10 + str[i][flag[j]] - '0';
    }
    mmin = min(mmin, s);
    mmax = max(mmax, s);
  }
  return mmax - mmin;
}
void dfs(int t) {
  if (t == k) {
    if (flag[0] == 2 && flag[1] == 0 && flag[2] == 3 && flag[3] == 1) {
      int a = 1;
      int b;
    }
    ans = min(ans, getans());
    return;
  }
  for (int i = 0; i < k; i++) {
    if (visit[i]) {
      continue;
    }
    visit[i] = true;
    flag[t] = i;
    dfs(t + 1);
    visit[i] = false;
  }
}
int main() {
  while (cin >> n >> k) {
    for (int i = 0; i < n; i++) {
      cin >> str[i];
    }
    memset(visit, 0, sizeof(visit));
    ans = inf;
    dfs(0);
    cout << ans << endl;
  }
  return 0;
}
