Problem: You are given n k-digit integers. You have to rearrange the digits in the integers so that the difference between the largest and the smallest number was minimum. Digits should be rearranged by the same rule in all integers.

Input

The first line contains integers n and k — the number and digit capacity of numbers correspondingly (1 ≤ n, k ≤ 8). Next n lines contain k-digit positive integers. Leading zeroes are allowed both in the initial integers and the integers resulting from the rearranging of digits.

Output

Print a single number: the minimally possible difference between the largest and the smallest number after the digits are rearranged in all integers by the same rule.

Examples

Input

6 4
5237
2753
7523
5723
5327
2537


Output

2700


Input

3 3
010
909
012


Output

3


Input

7 5
50808
36603
37198
44911
29994
42543
50156


Output

20522

Note

In the first sample, if we rearrange the digits in numbers as (3,1,4,2), then the 2-nd and the 4-th numbers will equal 5237 and 2537 correspondingly (they will be maximum and minimum for such order of digits).

In the second sample, if we swap the second digits and the first ones, we get integers 100, 99 and 102.

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
class UnionFind {
 private:
 public:
  vector<int> p, rank, setSize, minn;
  int numSets;
  UnionFind(int N) {
    setSize.assign(N, 1);
    numSets = N;
    rank.assign(N, 0);
    p.assign(N, 0);
    minn.resize(N);
    for (int i = 0; i < N; i++) {
      p[i] = i;
      minn[i] = i;
    }
  }
  int findSet(int i) {
    if (p[i] == i)
      return i;
    else {
      p[i] = findSet(p[i]);
      minn[p[i]] = min(minn[p[i]], i);
      return p[i];
    }
  }
  bool isSameSet(int i, int j) { return findSet(i) == findSet(j); }
  void unionSet(int i, int j) {
    if (!isSameSet(i, j)) {
      numSets--;
      int x = findSet(i), y = findSet(j);
      if (rank[x] > rank[y]) {
        p[y] = x;
        setSize[x] += setSize[y];
        minn[x] = min(minn[x], minn[y]);
      } else {
        p[x] = y;
        setSize[y] += setSize[x];
        minn[y] = min(minn[x], minn[y]);
        if (rank[x] == rank[y]) rank[y]++;
      }
    }
  }
  int numDisjointSets() { return numSets; }
  int sizeOfSet(int i) { return setSize[findSet(i)]; }
};
class fenwick_tree {
  vector<long> t;
  long size;

 public:
  fenwick_tree(long n) {
    t.assign(n + 1, 0);
    size = n + 1;
  }
  long LSOne(long k) { return k & (-k); }
  void update(long index, long val) {
    for (; index < size; index += LSOne(index)) t[index] += val;
  }
  long query_full(long a, long b) {
    return query(b) - ((a <= 1) ? 0 : query(a - 1));
  }
  long query(long b) {
    if (b < 1) return 0;
    long sum = 0;
    for (; b; b -= LSOne(b)) sum += t[b];
    return sum;
  }
};
long long gcd(long long a, long long b) {
  while (b != 0) {
    long long t = b;
    b = a % b;
    a = t;
  }
  return a;
}
int prime[1050000];
int lp[3001002];
class prime_class {
 public:
  long top;
  prime_class() {
    int i, i2, j;
    top = 0;
    lp[0] = 0;
    lp[1] = 1;
    for (i = 2; i < 3001002; i++) lp[i] = 0;
    top = 0;
    for (i = 2; i < 3001002; ++i) {
      if (lp[i] == 0) {
        lp[i] = i;
        prime[top++] = i;
      }
      for (j = 0; (j < top) && (prime[j] <= lp[i]) && (i * prime[j] < 3001002);
           ++j)
        lp[i * prime[j]] = prime[j];
    }
  }
  bool isprime(int key) {
    if (key < 3001002)
      return (lp[key] == key) && (key >= 2);
    else {
      int i;
      for (i = 0; (i < top) && (prime[i] * prime[i] <= key); i++)
        if (key % prime[i] == 0) return false;
      return true;
    }
  }
  unordered_map<int, int> factorize(int key) {
    unordered_map<int, int> ans;
    while (lp[key] != key) {
      ans[lp[key]]++;
      key /= lp[key];
    }
    if (key > 1) ans[key]++;
    return ans;
  }
  vector<pair<long long, long long>> factorize_full(long long key) {
    vector<pair<long long, long long>> ans;
    long i, ct;
    for (i = 0; (i < top) && (prime[i] <= key); i++)
      if (key % prime[i] == 0) {
        ct = 0;
        while (key % prime[i] == 0) {
          ct++;
          key /= prime[i];
        }
        ans.push_back({prime[i], ct});
      }
    if (key > 1) {
      ans.push_back({key, 1});
    }
    return ans;
  }
  void generate_divisors(int step, int v, vector<pair<int, int>>& fp,
                         vector<int>& ans) {
    if (step < fp.size()) {
      generate_divisors(step + 1, v, fp, ans);
      for (int i = 1; i <= fp[step].second; i++) {
        v *= fp[step].first;
        generate_divisors(step + 1, v, fp, ans);
      }
    } else
      ans.push_back(v);
  }
  void generate_divisors_full(long long step, long long v,
                              vector<pair<long long, long long>>& fp,
                              vector<long long>& ans) {
    if (step < fp.size()) {
      generate_divisors_full(step + 1, v, fp, ans);
      for (int i = 1; i <= fp[step].second; i++) {
        v *= fp[step].first;
        generate_divisors_full(step + 1, v, fp, ans);
      }
    } else
      ans.push_back(v);
  }
  vector<int> get_divisors(int key) {
    unordered_map<int, int> f = factorize(key);
    int n = f.size();
    vector<pair<int, int>> fp;
    for (auto p : f) fp.push_back(p);
    vector<int> ans;
    generate_divisors(0, 1, fp, ans);
    return ans;
  }
  vector<long long> get_divisors_full(long long key) {
    vector<pair<long long, long long>> f = factorize_full(key);
    int n = f.size();
    vector<pair<long long, long long>> fp;
    for (auto p : f) fp.push_back(p);
    vector<long long> ans;
    generate_divisors_full(0, 1, fp, ans);
    return ans;
  }
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m, i, j, k, ans;
  cin >> n >> k;
  vector<string> s(n);
  string ss;
  for (i = 0; i < n; i++) cin >> s[i];
  vector<int> p(k);
  for (i = 0; i < k; i++) p[i] = i;
  ans = 1000000000;
  do {
    vector<int> a;
    for (i = 0; i < n; i++) {
      ss = s[i];
      for (j = 0; j < k; j++) ss[j] = s[i][p[j]];
      a.push_back(stoi(ss));
    }
    sort(a.begin(), a.end());
    ans = min(ans, a[n - 1] - a[0]);
  } while (next_permutation(p.begin(), p.end()));
  cout << ans << "\n";
  return 0;
}