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.BufferedReader;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.*;

public class B92 {

	static boolean test = false;

	int n,k;
	int best = Integer.MAX_VALUE;
	String[] nums;
	private void solve() throws Throwable {
		
		n = iread();
		k = iread();
		nums = new String[n];
		
		for (int j = 0; j < n; j++) {
			nums[j] = wread();
		}
		
		go(0, new boolean[k], new int[k]);
		
		System.out.println(best);
	}
	private void go(int i, boolean[] bs, int[] res) {
		if(i == k){
			int min = Integer.MAX_VALUE;
			int max = 0;
			for (int j = 0; j < nums.length; j++) {
				String str = "";
				for (int k = 0; k < res.length; k++) {
					str += nums[j].charAt(res[k]);
				}
				Integer x = Integer.valueOf(str);
				min = Math.min(x, min);
				max = Math.max(max, x);
			}
			best = Math.min(best, max-min);
		}
		for (int j = 0; j < bs.length; j++) {
			if(!bs[j]){
				bs[j] = true;
				res[j] = i;
				go(i+1, bs, res);
				bs[j] = false;
			}
		}
	}

	public int iread() throws Exception {
		return Integer.parseInt(wread());
	}

	public double dread() throws Exception {
		return Double.parseDouble(wread());
	}

	public long lread() throws Exception {
		return Long.parseLong(wread());
	}

	public String wread() throws IOException {
		StringBuilder b = new StringBuilder();
		int c;
		c = in.read();
		while (c >= 0 && c <= ' ')
			c = in.read();
		if (c < 0)
			return "";
		while (c > ' ') {
			b.append((char) c);
			c = in.read();
		}
		return b.toString();
	}

	public static void main(String[] args) throws Throwable {
		new B92().solve();
		out.close();
	}

	public B92() throws Throwable {
		if (test) {
			in = new BufferedReader(new FileReader(new File(testDataFile)));
		} else {
			new BufferedReader(inp);
		}
	}

	static InputStreamReader inp = new InputStreamReader(System.in);
	static BufferedReader in = new BufferedReader(inp);
	static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));

	static String testDataFile = "testdata.txt";
	BufferedReader reader = null;
}