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.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Permutations124B {

	static char [] c;
	static ArrayList<ArrayList<String>> e;
	static int idx;
	
	public static void swap(int s, int e) {
		char temp = c[s];
		c[s] = c[e];
		c[e] = temp;
	}

	public static void nextPermutation(int s) { 

		if (s == c.length) { 
			if(e.size() > idx && e.get(idx) != null){
				e.get(idx).add(new String(c));
				idx++;
				return;
			}
			ArrayList<String> temp = new ArrayList<>();
			temp.add(new String(c));
			e.add(temp);
			idx++;
			return;
		}

		for (int j = s; j < c.length; j++) { // swaping
			swap(s, j);
			nextPermutation(s + 1);
			swap(s, j); 
						
		}
	}
	
	public static void main(String[] args) throws IOException {
		MyScanner sc = new MyScanner(System.in);
		PrintWriter pw = new PrintWriter(System.out);
		int n = sc.nextInt();
		int k = sc.nextInt();
		String [] s = new String[n];
		e = new ArrayList<>();
		for(int i = 0;i < n;i++){
			s[i] = sc.nextLine();
			c = s[i].toCharArray();
			idx = 0;
			nextPermutation(0);
		}
		int ans = (int)1e9;
		for(int i = 0;i < e.size();i++){
			int min = (int)1e9;
			int max = (int)-1e9;
			for(int j = 0;j < e.get(i).size();j++){
				min = Math.min(min, Integer.parseInt(e.get(i).get(j)));
				max = Math.max(max, Integer.parseInt(e.get(i).get(j)));
	
			}
			ans = Math.min(ans, max - min);		
		}
		pw.println(ans);
		pw.close();
	}

	static class MyScanner {
		StringTokenizer st;
		BufferedReader br;

		public MyScanner(InputStream s) {
			br = new BufferedReader(new InputStreamReader(s));
		}

		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		public long nextLong() throws IOException {
			return Long.parseLong(next());
		}

		public String nextLine() throws IOException {
			return br.readLine();
		}

		public double nextDouble() throws IOException {
			String x = next();
			StringBuilder sb = new StringBuilder("0");
			double res = 0, f = 1;
			boolean dec = false, neg = false;
			int start = 0;
			if (x.charAt(0) == '-') {
				neg = true;
				start++;
			}
			for (int i = start; i < x.length(); i++)
				if (x.charAt(i) == '.') {
					res = Long.parseLong(sb.toString());
					sb = new StringBuilder("0");
					dec = true;
				} else {
					sb.append(x.charAt(i));
					if (dec)
						f *= 10;
				}
			res += Long.parseLong(sb.toString()) / f;
			return res * (neg ? -1 : 1);
		}

		public boolean ready() throws IOException {
			return br.ready();
		}

	}
}