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

public class q2 {
    
    static class Scan {

        private final InputStream inputStream;
	private final byte[] buffer = new byte[1024];
	private int currentChar;
	private int numChars;
	private SpaceCharFilter filter;

	public Scan () {
            inputStream = System.in;
	}

	public int read () {
            if (numChars==-1)
		throw new InputMismatchException();

            if (currentChar >= numChars) {
		currentChar = 0;
		try {
                    numChars = inputStream.read(buffer);
		}
		catch (IOException e) {
                    throw new InputMismatchException();
		}
				
		if (numChars <= 0)				
                    return -1;
            }
            return buffer[currentChar++];
	}
	 
	public String nextLine () {
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            String str = "";
            
            try {
                str = br.readLine();
            }
            catch (IOException e) {
            }
            return str;
	}
	
        public int nextInt () {	
            int ch = read();
			
            while (isSpaceChar(ch)) 
		ch = read();
			
            int sgn = 1;
			
            if (ch == '-') {
		sgn = -1;
                ch = read();
            }
			
            int res = 0;
            do {
		if (ch < '0' || ch > '9') 
                    throw new InputMismatchException();
		res *= 10;
		res += ch - '0';
		ch = read();
            }
            while (!isSpaceChar(ch)); 
			
            return res * sgn;
	}
		
	public long nextLong () {
            int ch = read();
            
            while (isSpaceChar(ch))
		ch = read();
            
            int sgn = 1;
            
            if (ch == '-') {
		sgn = -1;
		ch = read();
            }
            
            long res = 0;
			
            do {
                if (ch < '0' || ch > '9')
                    throw new InputMismatchException();
		res *= 10;
		res += ch - '0';
		ch = read();
            }
            while (!isSpaceChar(ch));
            
            return res * sgn;
	}
		
	public double nextDouble () {
            int ch = read();
		
            while (isSpaceChar(ch))
		ch = read();
            
            int sgn = 1;
            if (ch == '-') {
		sgn = -1;
		ch = read();
            }
	
            double res = 0;
            while (!isSpaceChar(ch) && ch != '.') {
		if (ch == 'e' || ch == 'E')
                    return res * Math.pow(10, nextInt());
		if (ch < '0' || ch > '9')
                    throw new InputMismatchException();
		res *= 10;
		res += ch - '0';
		ch = read();
            }
            if (ch == '.') {
		ch = read();
		double m = 1;
		while (!isSpaceChar(ch)) {
                    if (ch == 'e' || ch == 'E')
			return res * Math.pow(10, nextInt());
                    if (ch < '0' || ch > '9')
                    	throw new InputMismatchException();
                    m /= 10;
                    res += (ch - '0') * m;
                    ch = read();
		}
            }
	
            return res * sgn;
	}
		
	public String readString () {
            int ch = read();
            while (isSpaceChar(ch))
		ch = read();
	
            StringBuilder res = new StringBuilder();
	
            do {
		res.appendCodePoint(ch);
		ch = read();
            } 
            while (!isSpaceChar(ch));
			
            return res.toString();
	}
	 
	public boolean isSpaceChar (int ch) {
            if (filter != null)
		return filter.isSpaceChar(ch);
            return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t' || ch == -1;
	}
	 
	public String next () {
            return readString();
	}

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);
	}
    }
    
    static class Print {
        private final BufferedWriter bw;
        
        public Print () {
            this.bw=new BufferedWriter(new OutputStreamWriter(System.out));
        }
        
        public void print (Object object)throws IOException {
            bw.append(""+object);
        }
        
        public void println (Object object)throws IOException {
            print(object);
            bw.append("\n");
        }
        
        public void close () throws IOException {
            bw.close();
        }
    }
    
    private static void permute (StringBuilder st, int l,int r) {
        if(l==r) {
            int min=Integer.MAX_VALUE, max=Integer.MIN_VALUE, num;
            for(int i=0;i<n;i++) {
                String s="";
                for(int j=0;j<st.length();j++) {
                    s+=str[i].charAt(st.charAt(j)-'0');
                }
                num=Integer.valueOf(s);
                if(num<min) 
                    min=num;
                if(num>max)
                    max=num;
            }
            min=max-min;
            if(min<f_min)
                f_min=min;
        }
        else {
            for(int i=l;i<=r;i++) {
                char ch1, ch2;
                ch1=st.charAt(i);
                ch2=st.charAt(l);
                st.setCharAt(i, ch2);
                st.setCharAt(l, ch1);
                permute(st, l+1, r);
                ch1=st.charAt(i);
                ch2=st.charAt(l);
                st.setCharAt(i, ch2);
                st.setCharAt(l, ch1);   
            }
        }
    }
    
    static String str[];
    static int n, f_min=Integer.MAX_VALUE;
    
    public static void main (String[] args) throws IOException {
        Scan sc = new Scan();
        Print pr = new Print();
        
        int k, i;
        n=sc.nextInt();
        k=sc.nextInt();
        
        str=new String[n];
        for(i=0;i<n;i++)
            str[i]=sc.next();
        StringBuilder ref=new StringBuilder("");
       
        for(i=0;i<k;i++) {
            ref.append(String.valueOf(i));
        }
        
        permute(ref, 0, ref.length()-1);
        pr.println(f_min);
        
        pr.close();
    }
    
}