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.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.Arrays;
import java.util.Locale;


public class Main {
    
    static void nextp(int[] a) {
        int i,j;
        int n = a.length;
        for (i=n-1;i>0;--i) {
            if (a[i]>a[i-1]) {
                for (j=n-1;j>i;--j)
                    if (a[j]>a[i-1]) break;
              
                int tmp = a[i-1]; a[i-1] = a[j]; a[j] = tmp; // swap 
              
                break;
            }
        }
        for(j=n-1;j>i;--j,++i) {
            int tmp = a[i]; a[i] = a[j]; a[j] = tmp; // swap 
        }
    }
    
    public static void main(String[] args)throws Exception {
        String[] s = inB.readLine().split(" ");
        int n = Integer.parseInt(s[0]);
        int k = Integer.parseInt(s[1]);
        
        int[][] c = new int[n][k];
        
        for(int i = 0; i<n; i++) {
            char[] cc = inB.readLine().toCharArray();
            for(int j = 0; j<k; j++) {
                c[i][j] = cc[j] - '0';
            }
        }
        
        int[] pt = new int[k];
        for(int i = 0; i<k; i++)pt[i] = i+1;
        
        int fact = 1;
        for(int i = 1; i<=k; i++)fact *= i;
        
        int min = INF;
        for(int i = 0; i<fact; i++, nextp(pt)) {
            int[] curmas = new int[n];
            for(int j = 0; j<n; j++) {
                for(int ii = 0, dec=1; ii<k; ii++, dec *= 10) {
                    curmas[j] += c[j][pt[ii]-1]*dec;
                }
            }
            Arrays.sort(curmas);
            if(curmas[n-1] - curmas[0] < min)
                min = curmas[n-1] - curmas[0];
            
            
        }
        
        println(min);
        //out.flush();
    }
    
    /////////////////////////////////////////////////////////////////
    // IO
    /////////////////////////////////////////////////////////////////
    private static StreamTokenizer in;
    private static PrintWriter out;
    private static BufferedReader inB;
    
    private static boolean FILE=false;
    
    private static int nextInt() throws Exception{
        in.nextToken();
        return (int)in.nval;
    }
    
    private static String nextString() throws Exception{
        in.nextToken();
        return in.sval;
    }
    
    static{
        try {
            out = new PrintWriter(FILE ? (new FileOutputStream("output.txt")) : System.out);
            inB = new BufferedReader(new InputStreamReader(FILE ? new FileInputStream("input.txt") : System.in));
        } catch(Exception e) {e.printStackTrace();}
        in = new StreamTokenizer(inB);
    }
    /////////////////////////////////////////////////////////////////
    
    /////////////////////////////////////////////////////////////////
    // pre - written
    /////////////////////////////////////////////////////////////////
    private static void println(Object o) throws Exception {
        out.println(o);
        out.flush();
    }
    private static void exit(Object o) throws Exception {
        println(o);
        exit();
    }
    private static void exit() {
        System.exit(0);
    }
    private static final int  INF = Integer.MAX_VALUE;
    private static final int MINF = Integer.MIN_VALUE;
    //////////////////////////////////////////////////////////////////
}