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.io.*;
import java.util.*;
public class Permutations {
    static BufferedReader br;
    static String[] numbers;
    static int k;
    public static void main(String[] args) {
        try {
            br = new BufferedReader(new InputStreamReader(System.in));
            init();
            System.out.println(solve());
            br.close();
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
    private static void init() throws IOException {
    	StringTokenizer st = new StringTokenizer(br.readLine());
    	numbers = new String[Integer.parseInt(st.nextToken())];
    	k = Integer.parseInt(st.nextToken());
    	for(int i = 0; i < numbers.length; i++)
    		numbers[i] = br.readLine();
    }
    private static String solve() {
    	int min = Integer.MAX_VALUE;
    	String s1 = "";
    	for(int i = 0; i < k; i++)
    		s1 += i + "";
    	String[] s = possibleDigits(s1);
        String[] changeable = new String[numbers.length];
        for(int i = 0; i < changeable.length; i++)
        	changeable[i] = "";
    	for(int i = 0; i < s.length; i++) {
    		int total = 0;
    		for(int j = 0; j < changeable.length; j++) {
    			for(int k = 0; k < numbers[j].length(); k++) {
    				String a = s[i].substring(k, k + 1);
    				changeable[j] += numbers[j].substring(Integer.parseInt(a), Integer.parseInt(a) + 1);
    			}
    		}
    		Arrays.sort(changeable);
    		total = Integer.parseInt(changeable[changeable.length - 1]) - Integer.parseInt(changeable[0]);
    		if(total < min)
    			min = total;
    		changeable = new String[numbers.length];
    		for(int j = 0; j < changeable.length; j++)
    			changeable[j] = "";
    	}
    	return String.valueOf(min);
    }
    private static String[] possibleDigits(String s) {
    	ArrayList<String> list = new ArrayList<String>();
    	list.add(s.substring(0, 1));
    	for(int i = 1; i < s.length(); i++) {
    		for(int j = list.size() - 1; j >= 0; j--) {
    			String str = list.remove(j);
    			for(int k = 0; k <= str.length(); k++)
    				list.add(str.substring(0, k) + s.charAt(i) + str.substring(k));
    		}
    	}
    	String[] res = new String[list.size()];
    	for(int i = 0; i < list.size(); i++)
    		res[i] = list.get(i);
    	return res;
    }
}