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 JAVA):

Solution:
import java.util.Arrays;
import java.util.Scanner;


import java.math.BigInteger;

public class anuj
    {
    public static void main(String args[])
        {
        Scanner c=new Scanner(System.in);
        int n=c.nextInt();
        int k=c.nextInt();
        String A[]=new String[n];
        for(int i=0;i<n;i++)
            A[i]=c.next();
        char C[][]=new char[n][k];
        for(int i=0;i<n;i++)
            C[i]=A[i].toCharArray();
        PermutationGenerator x = new PermutationGenerator(k);
        int min=1000000000;
        while(x.hasMore())
            {
            int indices[];
            indices=x.getNext();
            //for(int i=0;i<k;i++)
                //System.out.print(indices[i]+" ");
            //System.out.println();
            char Ctemp[][]=new char[n][k];
            String Stemp[]=new String[n];
            int Atemp[]=new int[n];
            for(int i=0;i<n;i++)
                {
                Stemp[i]="";
                for(int j=0;j<k;j++)
                    {
                    Ctemp[i][j]=C[i][indices[j]];
                    Stemp[i]=Stemp[i].concat(Character.toString(Ctemp[i][j]));
                    //System.out.print(Ctemp[i][j]);
                    }
                //System.out.println();
                }
            //System.out.println("\n\n");
            Arrays.sort(Stemp);
            for(int i=0;i<n;i++)
                {
                Atemp[i]=Integer.parseInt(Stemp[i]);
                //System.out.println(Stemp[i]);
                }
            if(Atemp[n-1]-Atemp[0]<min)
                min=Atemp[n-1]-Atemp[0];
            //System.out.println("min here= "+(Atemp[n-1]-Atemp[0])+"\n\n");
            }
        System.out.println(min);
        }
    }

class PermutationGenerator 
{
private int[] a;
private BigInteger numLeft;
private BigInteger total;

public PermutationGenerator (int n) 
    {
    if (n < 1) 
            {
  throw new IllegalArgumentException ("Min 1");
}
a = new int[n];
total = getFactorial (n);
reset ();
}

//------
// Reset
//------

public void reset () {
for (int i = 0; i < a.length; i++) {
  a[i] = i;
}
numLeft = new BigInteger (total.toString ());
}

//------------------------------------------------
// Return number of permutations not yet generated
//------------------------------------------------

public BigInteger getNumLeft () {
return numLeft;
}

//------------------------------------
// Return total number of permutations
//------------------------------------

public BigInteger getTotal () {
return total;
}

//-----------------------------
// Are there more permutations?
//-----------------------------

public boolean hasMore () {
return numLeft.compareTo (BigInteger.ZERO) == 1;
}

//------------------
// Compute factorial
//------------------

private static BigInteger getFactorial (int n) {
BigInteger fact = BigInteger.ONE;
for (int i = n; i > 1; i--) {
  fact = fact.multiply (new BigInteger (Integer.toString (i)));
}
return fact;
}

//--------------------------------------------------------
// Generate next permutation (algorithm from Rosen p. 284)
//--------------------------------------------------------

public int[] getNext () {

if (numLeft.equals (total)) {
  numLeft = numLeft.subtract (BigInteger.ONE);
  return a;
}

int temp;

// Find largest index j with a[j] < a[j+1]

int j = a.length - 2;
while (a[j] > a[j+1]) {
  j--;
}

// Find index k such that a[k] is smallest integer
// greater than a[j] to the right of a[j]

int k = a.length - 1;
while (a[j] > a[k]) {
  k--;
}

// Interchange a[j] and a[k]

temp = a[k];
a[k] = a[j];
a[j] = temp;

// Put tail end of permutation after jth position in increasing order

int r = a.length - 1;
int s = j + 1;

while (r > s) {
  temp = a[s];
  a[s] = a[r];
  a[r] = temp;
  r--;
  s++;
}

numLeft = numLeft.subtract (BigInteger.ONE);
return a;

}

}