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;
long long int MOD = 1e9 + 7;
long long int INF = 1e18;
long long int sub(long long int a, long long int b, long long int p = MOD) {
  return ((a % p) - (b % p) + p) % p;
}
long long int mult(long long int a, long long int b, long long int p = MOD) {
  return ((a % p) * (b % p)) % p;
}
long long int add(long long int a, long long int b, long long int p = MOD) {
  return (a % p + b % p) % p;
}
long long int fpow(long long int n, long long int k, long long int p = MOD) {
  long long int r = 1;
  while (k > 0) {
    if (k & 1) r = r * n % p;
    n = n * n % p;
    k = k >> 1;
  }
  return r;
}
long long int inv(long long int a, long long int p = MOD) {
  return fpow(a, p - 2, p);
}
long long int power(long long int a, long long int b) {
  long long int ans = 1;
  long long int i;
  for (i = 1; i < b + 1; i++) {
    ans *= a;
  }
  return ans;
}
long long int fdiv(long long int a, long long int b, long long int p = MOD) {
  long long int yinv = inv(b);
  long long int ans = (a * yinv) % p;
  return ans;
}
unsigned long long int I1018 = 1000000000000000000;
const unsigned long long int I10e5 = 100000;
const long long int I10e6 = 1e6;
template <typename T>
istream &operator>>(istream &in, vector<T> &a) {
  for (auto &item : a) {
    in >> item;
  }
  return in;
}
template <typename T, typename U>
ostream &operator<<(ostream &out, pair<T, U> &a) {
  cout << a.first << " " << a.second;
  return out;
}
template <typename T, typename U>
istream &operator>>(istream &out, pair<T, U> &a) {
  cin >> a.first >> a.second;
  return out;
}
template <typename T, typename U>
ostream &operator<<(ostream &out, map<T, U> &a) {
  for (auto &item : a) {
    out << item << endl;
  }
  return out;
}
template <typename T>
ostream &operator<<(ostream &out, vector<T> &a) {
  for (auto &item : a) {
    out << item << " ";
  }
  return out;
}
template <typename T>
ostream &operator<<(ostream &out, vector<vector<T>> &a) {
  for (auto &item : a) {
    out << item << "\n";
  }
  return out;
}
std::vector<bool> is_prime;
std::vector<long long int> primes;
void sieve(long long int n) {
  is_prime.resize(n + 2, true);
  primes.clear();
  long long int p;
  for (p = 2; p * p <= n; p++) {
    if (is_prime[p]) {
      long long int i;
      for (i = p * p; i <= n; i += p) {
        is_prime[i] = false;
      }
    }
  }
  is_prime[0] = is_prime[1] = false;
  long long int i;
  for (i = 2; i <= n; i++) {
    if (is_prime[i]) {
      primes.emplace_back(i);
    }
  }
}
map<long long int, long long int> prime_factors(long long int n) {
  map<long long int, long long int> s;
  long long int i;
  long long int tc = 0;
  while (n % 2 == 0) {
    tc++;
    n /= 2;
  }
  if (tc > 0) {
    s[2] = tc;
  }
  for (i = 3; i <= sqrt(n); i += 2) {
    tc = 0;
    while (n % i == 0) {
      tc++;
      n /= i;
    }
    if (tc > 0) {
      s[i] = tc;
    }
  }
  if (n > 2) {
    s[n] += 1;
  }
  return s;
}
std::vector<long long int> fact_vec;
void fact_fun(long long int n) {
  fact_vec.resize(n + 10);
  long long int i;
  fact_vec[0] = 1;
  for (i = 1; i <= n + 2; i++) {
    fact_vec[i] = (fact_vec[i - 1] * i) % MOD;
  }
}
long long int npr(long long int n, long long int r) {
  long long int ans = fact_vec[n];
  ans /= fact_vec[n - r];
  return ans;
}
long long int ncr(long long int n, long long int r) {
  long long int ans = fact_vec[n];
  ans /= fact_vec[n - r];
  ans /= fact_vec[r];
  return ans;
}
long long int nc2(long long int n) { return (n * (n - 1)) / 2; }
long long int mn = INF, mx = -INF;
long long int n, k;
void perm(string s, std::vector<long long int> p) {
  long long int i;
  string np = s;
  for (i = 0; i < k; i++) {
    np[i] = s[p[i]];
  }
  long long int num = stoll(np);
  mn = min(mn, num);
  mx = max(mx, num);
}
int main() {
  long long int i, j;
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> k;
  std::vector<string> v(n);
  cin >> v;
  std::vector<long long int> p(k);
  for (i = 0; i < k; i++) {
    p[i] = i;
  }
  long long int ans = INF;
  do {
    mn = INF;
    mx = -INF;
    for (auto s : v) {
      perm(s, p);
    }
    ans = min(ans, mx - mn);
  } while (next_permutation((p).begin(), (p).end()));
  cout << ans << endl;
}