#include <bits/stdc++.h>
using namespace std;
string numbers[10], arr, changer[10];
const int INF = 666666666;
int a, b, least;
int toi(string m) {
  int k = 0;
  for (int i = 0; i < m.length(); i++) {
    k = k * 10 + m[i] - '0';
  }
  return k;
}
int lea() {
  for (int i = 0; i < a; i++) {
    changer[i] = "";
    for (int j = 0; j < b; j++) {
      changer[i] = changer[i] + numbers[i][arr[j] - '0'];
    }
  }
  sort(changer, changer + a);
  return toi(changer[a - 1]) - toi(changer[0]);
}
int main() {
  while (cin >> a >> b) {
    for (int i = 0; i < a; i++) {
      cin >> numbers[i];
    }
    arr = "";
    for (int j = 0; j < b; j++) {
      arr = char(j + '0') + arr;
    }
    least = INF;
    sort(arr.begin(), arr.end());
    do {
      least = min(least, lea());
    } while (next_permutation(arr.begin(), arr.end()));
    cout << least << endl;
  }
}
