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.*;
import java.io.*;
import java.lang.Math.*;

public  class permutations {
	
	
	static int minDist = Integer.MAX_VALUE; 
	static int[][] numbers;

	public static void main(String[] args) {

	   Scanner infile = new Scanner(System.in);

     	int n  = infile.nextInt();
     	int k = infile.nextInt();
     	//how program works
     	//1. given k-bound => generate ALL possible digit combinations
     	/// ==== > do integer positions rather than going THROUGH EACH NUMBER => apply a mapping
     	//2. sub in with array values and keep track of a minimum
 		numbers = new int[n][k]; //better storage mechanism
     	for(int a = 0; a < n; a++)
     	{
     		String num = infile.next().trim();
     		for(int b = 0; b < k; b++)
     		{
     			int digit  = (int)(num.charAt(b));
     			numbers[a][b] = digit -'0';
     		}
     	}

     	Set<Integer> values = new HashSet<Integer>();
     	for(int a = 1; a <= k; a++)
     		values.add(a);

	   	genPerms(0, new int[k], values);
     	/*int[] testPerm = {3,2,1}; 
     	int[] originalVal = {2,0,1}; 
     	int[] res = swap(originalVal,testPerm);
		for(int a = 0; a < res.length; a++)
     		System.out.println(res[a] + " ");
		System.out.println(arrToSum(res));
		*/
		System.out.println(minDist);


	}

	public static void genPerms(int pos, int[] perm, Set<Integer> remaining)
	{
		if(remaining.size() == 0) //doen with permutation generation
		{
			int[] tempNums = new int[numbers.length];
			int tempMin = Integer.MAX_VALUE;
			int tempMax = Integer.MIN_VALUE;
			for(int i = 0; i < numbers.length; i++)
			{
				tempNums = swap(numbers[i], perm);
				int actRep = arrToSum(tempNums);
				 //System.out.println("actRep = " + actRep);
				if(actRep < tempMin) tempMin = actRep;
				if(actRep > tempMax) tempMax = actRep;
			}
			int tempDist = tempMax - tempMin;
			// System.out.println("tempDist = " + tempDist);
			// System.out.println("-------------------------");
			if(tempDist < minDist) 
				{
					minDist = tempDist;
					// System.out.println("minDist = " + minDist);
				}
			return;
		}
		for(int i : new HashSet<Integer>(remaining))  //make a new set => can't modify existing!!
		{
			remaining.remove(i);
			perm[pos] = i;
			genPerms(pos+1,perm,remaining);
			remaining.add(i);
		}
	}

// 	for (i = 0; i < 4; i++) {
//   numberArray[i] = number%10;
//   number = number div 10;
// }

	public static int[] swap(int[] num, int[] perm) 
	{
		//convert number to a number array
		int[] numArr = new int[perm.length]; 
		for(int i = 0; i < perm.length; i++)
			numArr[i] = num[perm[i] - 1];
		return numArr;			
	}

	public static int arrToSum(int[] num)
	{
		int result = 0;
		for(int i = num.length - 1; i >= 0; i--)
			result += num[i] * Math.pow(10,(num.length - 1 -i));
		return result;
	}

	// public static int swap(int num, int[] perm) //assume size of int[] perm = lesngth of num
	// {
	// 	String s = num + "";
	// 	char[] newNums = new char[perm.length]; //assume as "1 2 3"
	// 	for(int i = 0; i < s.length(); i++)
	// 	{
	// 		System.out.println(s.charAt(perm[i] - 1));
	// 		newNums[i]  = s.charAt(perm[i] - 1);
	// 	}
			
	// 	String newVal = String.valueOf(newNums);
	// 	System.out.println("newVal = " + newVal);		
	// 	int actualValue = Integer.parseInt(newVal);
	// 	return actualValue;
	// }

}