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;
vector<string> v2;
vector<vector<string> > v1;
int stringToInt(string s) {
  int sum = 0;
  int l = s.length() - 1;
  for (int i = 0; i < s.length(); i++) {
    sum += (s[i] - 48) * pow(10.0, l--);
  }
  return sum;
}
void permute(string s, int l, int h) {
  int x;
  if (l == h) {
    v2.push_back(s);
  } else {
    for (int i = l; i < h; i++) {
      swap(s[l], s[i]);
      permute(s, l + 1, h);
      swap(s[l], s[i]);
    }
  }
}
int main() {
  int n, k, x;
  cin >> n >> k;
  string stringstream;
  char c[k];
  vector<string> v;
  for (int i = 0; i < n; i++) {
    cin >> stringstream;
    v.push_back(stringstream);
  }
  for (int i = 0; i < k; i++) c[i] = (i + 1) + 48;
  c[k] = 0;
  string s = c;
  permute(s, 0, k);
  vector<int> v3;
  int xx;
  int min = 99999999;
  for (int i = 0; i < v2.size(); i++) {
    v3.clear();
    for (int j = 0; j < v.size(); j++) {
      char ch[k];
      for (int kk = 0; kk < k; kk++) ch[kk] = v[j][(v2[i][kk] - 48) - 1];
      ch[k] = 0;
      string pat = ch;
      xx = stringToInt(pat);
      v3.push_back(xx);
    }
    sort(v3.begin(), v3.end());
    if (v3[v3.size() - 1] - v3[0] < min) {
      min = v3[v3.size() - 1] - v3[0];
    }
  }
  cout << min << endl;
  return 0;
}