#include <bits/stdc++.h>
using namespace std;
const int N = 10;
int data[N][N];
int main() {
  string adj[N];
  int n, m, imin = 1 << 30;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    string str;
    cin >> str;
    for (int j = 0; j < m; j++) {
      adj[j] += str[j];
    }
  }
  int permu[N];
  for (int i = 0; i < m; i++) permu[i] = i;
  do {
    map<int, int> order;
    int data[N];
    for (int i = 0; i < m; i++) {
      order[permu[i]] = i;
      data[i] = i;
    }
    string tmp[N];
    for (int i = 0; i < m; i++) tmp[i] = adj[i];
    for (int i = 0; i < m - 1; i++) {
      for (int j = 0; j < m - i - 1; j++) {
        if (order[data[j]] > order[data[j + 1]]) {
          swap(data[j], data[j + 1]);
          swap(tmp[j], tmp[j + 1]);
        }
      }
    }
    int digit[N] = {0};
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        digit[i] = digit[i] * 10 + tmp[j][i] - '0';
      }
    }
    sort(digit, digit + n);
    imin = min(imin, digit[n - 1] - digit[0]);
  } while (next_permutation(permu, permu + m));
  cout << imin << endl;
  return 0;
}
