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;
template <class T>
inline T lowbit(T n) {
  return (n ^ (n - 1)) & n;
}
template <class T>
inline int countbit(T n) {
  return (n == 0) ? 0 : (1 + countbit(n & (n - 1)));
}
template <class T>
T gcd(T a, T b) {
  return b == 0 ? a : gcd(b, a % b);
}
template <typename T>
T lcm(T a, T b) {
  return a / gcd(a, b) * b;
}
template <class T>
T my_pow(T n, T p) {
  if (p == 0) return 1;
  T x = my_pow(n, p / 2);
  x = (x * x);
  if (p & 1) x = (x * n);
  return x;
}
template <class T>
T big_mod(T n, T p, T m) {
  if (p == 0) return (T)1;
  T x = big_mod(n, p / 2, m);
  x = (x * x) % m;
  if (p & 1) x = (x * n) % m;
  return x;
}
template <class T>
inline T Mod(T n, T m) {
  return (n % m + m) % m;
}
template <class T>
T multiplication(T n, T p, T m) {
  if (p == 0) return (T)0;
  T x = multiplication(n, p / 2, m);
  x = (x + x) % m;
  if (p & 1) x = (x + n) % m;
  return x;
}
bool isvowel(char ch) {
  ch = toupper(ch);
  if (ch == 'A' || ch == 'U' || ch == 'I' || ch == 'O' || ch == 'E')
    return true;
  return false;
}
template <class T>
T extract(string s, T ret) {
  stringstream ss(s);
  ss >> ret;
  return ret;
}
template <class T>
string itos(T n) {
  ostringstream ost;
  ost << n;
  ost.flush();
  return ost.str();
}
long long stoi(string s) {
  long long r = 0;
  istringstream sin(s);
  sin >> r;
  return r;
}
double toDouble(string s) {
  double r = 0;
  istringstream sin(s);
  sin >> r;
  return r;
}
int col[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int row[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const int mod = 1e9 + 7;
int a = 0, b = 0, c = 0, d = 0, cnt = 0, r = 0, rr = 0, f = 0, ff = 0, tot = 0,
    sum = 0, res = 0, n, m, k = 0, ks = 0, t = 0, tt = 0;
long long mn = (1e15);
long long mx = -(1e15);
long long ar[654321];
vector<long long> v;
string s[50], ss;
long long dp[555][100];
int main() {
  cin >> n >> m;
  for (__typeof(n) i = 0; i < (n); i++) {
    cin >> s[i];
    for (__typeof(m) j = 0; j < (m); j++) {
      dp[i][j] = (s[i][j] - '0');
    }
  }
  for (__typeof(m) i = 0; i < (m); i++) ar[i] = i;
  long long diff = (1e15);
  long long ans = 0;
  do {
    long long res = 0;
    mx = 0, mn = (1e15);
    for (__typeof(n) i = 0; i < (n); i++) {
      ans = 0;
      for (__typeof(m) j = 0; j < (m); j++) {
        ans = (ans * 10) + dp[i][ar[j]];
      }
      mx = max(mx, ans);
      mn = min(mn, ans);
    }
    diff = min(diff, mx - mn);
    c++;
  } while (next_permutation(ar, ar + m));
  cout << diff << endl;
  return 0;
}