#include <bits/stdc++.h>
using namespace std;
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T>
void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v)) cerr << ", ";
  _print(v...);
}
const int mod = 1000000007;
int mpow(int b, int e);
int n, k, ans = INT_MAX;
void helper(int l, vector<string> &v) {
  if (l == k) {
    string minn = v[0], maxx = v[0];
    for (int i = 1; i < n; i++) {
      minn = min(v[i], minn);
      maxx = max(v[i], maxx);
    }
    ans = min(ans, stoi(maxx) - stoi(minn));
    return;
  }
  for (int i = l; i < k; i++) {
    for (auto &s : v) swap(s[l], s[i]);
    helper(l + 1, v);
    for (auto &s : v) swap(s[l], s[i]);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> k;
  vector<string> v;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    string s = to_string(x);
    while (s.size() != k) s = '0' + s;
    v.emplace_back(s);
  }
  if (n == 1) {
    cout << 0 << endl;
    return 0;
  }
  helper(0, v);
  cout << ans << endl;
  return 0;
}
int mpow(int base, int exp) {
  base %= mod;
  int result = 1;
  while (exp > 0) {
    if (exp & 1) result = ((long long int)result * base) % mod;
    base = ((long long int)base * base) % mod;
    exp >>= 1;
  }
  return result;
}
