#include <bits/stdc++.h>
using namespace std;
int n, k, tmp[10];
string dat[10], cur[10], ans;
string sub(string a, string b) {
  string res = a;
  int pm = 0, m = 0;
  for (int i = (int)a.size() - 1; i >= 0; i--) {
    int ai = a[i] - '0', bi = b[i] - '0';
    if (ai < bi - pm) {
      ai += 10;
      m = -1;
    } else {
      m = 0;
    }
    res[i] = (ai - bi + pm) + '0';
    pm = m;
  }
  return res;
}
int main() {
  cin >> n >> k;
  if (n == 1) {
    cout << 0 << endl;
    return 0;
  }
  for (int i = 0; i < n; i++) {
    cin >> dat[i];
    cur[i] = dat[i];
  }
  ans = dat[0];
  for (int i = 0; i < k; i++) {
    tmp[i] = i;
    ans[i] = '9';
  }
  do {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < k; j++) {
        cur[i][j] = dat[i][tmp[j]];
      }
    }
    sort(cur, cur + n);
    string maxi = cur[n - 1], mini = cur[0];
    ans = min(ans, sub(maxi, mini));
  } while (next_permutation(tmp, tmp + k));
  int i = 0;
  while (ans[i] == '0') {
    ans.erase(ans.begin() + i);
  }
  cout << ans << endl;
}
