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;
int main() {
  int a[10], b[10], c[10], d[10], a1[10], a2[10], a3[10], a4[10], a5[10];
  int m, n, max = 0, min = 100000000, oo;
  vector<int> vecaux;
  cin >> m >> n;
  n++;
  string A[10];
  for (int oi = 0; oi < m; oi++) {
    cin >> A[oi];
  }
  for (int i = 1; i < n; i++) {
    for (oo = 0; oo < m; oo++) a[oo] = A[oo][i - 1] - 48;
    for (oo = 0; oo < m; oo++) d[oo] = a[oo];
    for (int j = 1; j < n; j++) {
      for (oo = 0; oo < m; oo++) c[oo] = a[oo];
      if (n > 2) {
        if (j == i) continue;
        for (oo = 0; oo < m; oo++) a[oo] = a[oo] * 10 + A[oo][j - 1] - 48;
      }
      for (int z = 1; z < n; z++) {
        for (oo = 0; oo < m; oo++) b[oo] = a[oo];
        if (n > 3) {
          if (z == i) continue;
          if (z == j) continue;
          for (oo = 0; oo < m; oo++) a[oo] = a[oo] * 10 + A[oo][z - 1] - 48;
        }
        for (int ii = 1; ii < n; ii++) {
          for (oo = 0; oo < m; oo++) a1[oo] = a[oo];
          if (n > 4) {
            if (ii == i) continue;
            if (ii == j) continue;
            if (ii == z) continue;
            for (oo = 0; oo < m; oo++) a[oo] = a[oo] * 10 + A[oo][ii - 1] - 48;
          }
          for (int jj = 1; jj < n; jj++) {
            for (oo = 0; oo < m; oo++) a2[oo] = a[oo];
            if (n > 5) {
              if (jj == i) continue;
              if (jj == j) continue;
              if (jj == z) continue;
              if (jj == ii) continue;
              for (oo = 0; oo < m; oo++)
                a[oo] = a[oo] * 10 + A[oo][jj - 1] - 48;
            }
            for (int zz = 1; zz < n; zz++) {
              for (oo = 0; oo < m; oo++) a3[oo] = a[oo];
              if (n > 6) {
                if (zz == i) continue;
                if (zz == j) continue;
                if (zz == z) continue;
                if (zz == ii) continue;
                if (zz == jj) continue;
                for (oo = 0; oo < m; oo++)
                  a[oo] = a[oo] * 10 + A[oo][zz - 1] - 48;
              }
              for (int zza = 1; zza < n; zza++) {
                for (oo = 0; oo < m; oo++) a4[oo] = a[oo];
                if (n > 7) {
                  if (zza == i) continue;
                  if (zza == j) continue;
                  if (zza == z) continue;
                  if (zza == ii) continue;
                  if (zza == jj) continue;
                  if (zza == zz) continue;
                  for (oo = 0; oo < m; oo++)
                    a[oo] = a[oo] * 10 + A[oo][zza - 1] - 48;
                }
                for (int zzb = 1; zzb < n; zzb++) {
                  for (oo = 0; oo < m; oo++) a5[oo] = a[oo];
                  if (n > 8) {
                    if (zzb == i) continue;
                    if (zzb == j) continue;
                    if (zzb == z) continue;
                    if (zzb == ii) continue;
                    if (zzb == jj) continue;
                    if (zzb == zz) continue;
                    if (zzb == zza) continue;
                    for (oo = 0; oo < m; oo++)
                      a[oo] = a[oo] * 10 + A[oo][zzb - 1] - 48;
                  }
                  for (oo = 0; oo < m; oo++) {
                    if (a[oo] > max) max = a[oo];
                    if (a[oo] < min) min = a[oo];
                  }
                  vecaux.push_back(max - min);
                  max = 0;
                  min = 100000000;
                  for (oo = 0; oo < m; oo++) a[oo] = a5[oo];
                }
                for (oo = 0; oo < m; oo++) a[oo] = a4[oo];
              }
              for (oo = 0; oo < m; oo++) a[oo] = a3[oo];
            }
            for (oo = 0; oo < m; oo++) a[oo] = a2[oo];
          }
          for (oo = 0; oo < m; oo++) a[oo] = a1[oo];
        }
        for (oo = 0; oo < m; oo++) a[oo] = b[oo];
      }
      for (oo = 0; oo < m; oo++) a[oo] = c[oo];
    }
    for (oo = 0; oo < m; oo++) a[oo] = d[oo];
  }
  sort(vecaux.begin(), vecaux.begin() + vecaux.size());
  cout << vecaux[0] << endl;
  return 0;
}