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 Permutation {
	
	static SortedSet<String> perm;
	static int n,k;
	
	static String toString(int[] idx) {
		StringBuilder sb = new StringBuilder("");
		for(int i=0;i<k;i++)
			sb.append(""+idx[i]);
		return sb.toString();
		
	}
	
	static void permutations(int[] idx,int i) {
		if(!perm.contains(toString(idx))) perm.add(toString(idx));
		if(i<k-1) permutations(idx,i+1);
		for(int j=i+1;j<k;j++) {
			swap(idx,i,j);
			permutations(idx,i+1);
			swap(idx,j,i);
		}
	}
	
	static void swap(int idx[],int a, int b) {
		int remp = idx[b];
		idx[b] = idx[a];
		idx[a] = remp;
	}

	public static void main(String[] args) {
        FastScanner in = new FastScanner();
        PrintWriter out = new PrintWriter(System.out);
        n = in.nextInt(); k = in.nextInt();
        char ch[][] = new char[n][k];
        for(int i=0;i<n;i++) {
        	ch[i] = in.next().toCharArray();
        }
        perm = new TreeSet<>();
        int idx[] = new int[k];
        for(int i=1;i<=k;i++) idx[i-1] = i;
        for(int i=0;i<k;i++) {
        	swap(idx,0,i);
        	permutations(idx,0);
        }
        int ans = Integer.MAX_VALUE;
        for(String str : perm) {
        	int max = 0, min =Integer.MAX_VALUE;
        	char c[] = str.toCharArray();
        	for(int i=0;i<n;i++) {
        		int x = 0;
        		for(char y : c) {
        			x = 10*x + (ch[i][(y-'1')]-'0');
        		}
        		min = Math.min(min, x);
        		max = Math.max(max, x);
        	}
        	ans = Math.min(ans, max-min);
        }
        out.println(ans);
        out.flush();
	}
	
	static class FastScanner {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer("");
		
		String next() {
			while(!st.hasMoreTokens())
				try {
					st = new StringTokenizer(br.readLine());
				}catch(IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		}
		
		int nextInt() {
			return Integer.parseInt(next());
		}
		
		long nextLong() {
			return Long.parseLong(next());
		}
		
		int[] readArray(int n) {
			int a[] = new int[n];
			for(int i=0;i<n;i++) a[i] = nextInt();
			return a;
		}
		
		Integer[] readArray(long n) {
		    Integer a[] = new Integer[(int)n];
			for(int i=0;i<n;i++) a[i] = nextInt();
			return a;
		}
		
		long[] readArray(int n,long x) {
			long a[] = new long[n];
			for(int i=0;i<n;i++) a[i] = nextLong();
			return a;
		}
		
	}
	
	static class Pair implements Comparable<Pair> {
		int x, y;
		Pair(int a, int b){ x = a; y = b; }
		int getKey() { return x; }
		int getVal() { return y; }
		@Override
		public int compareTo(Pair o) {
			if(o.getVal() - this.getVal()>0)
				return -1;
			else if(o.getVal() - this.getVal()<0)
				return 1;
			else 
				return 0;
		}
		
	}
	
	static boolean arrayEquals(char a[], char b[]) {
		int n = a.length;
		boolean verdict = true;
		for(int i=0;i<n;i++) {
			if(a[i]!=b[i]) {
				verdict = false;break;
			}
		}
		return verdict;
	}
	
	static long lcm(long a, long b) {
		return (a*b)/gcd(a,b);
	}
	
	static long gcd(long a, long b) {
		if(b==0) return a;
		else return gcd(b,a%b);
	}
	
	static long hashInt(int x,int y) {
		return x*(1_000_000_000L)+y;
	}
}