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 java.nio.*;
import java.nio.channels.*;

public class Main {

    static BufferedReader reader;
    static StringTokenizer tokenizer;
    static PrintWriter out = new PrintWriter(Channels.newWriter(Channels.newChannel(System.out), "US-ASCII") );


    public static void init(InputStream input) {
        reader = new BufferedReader(new InputStreamReader(input) );
        tokenizer = new StringTokenizer("");
    }

    public static boolean hasNext() {
        boolean eof = false;
        String line = null;
        
        try {
            while (!eof && !tokenizer.hasMoreTokens()) {
                line = reader.readLine();
                
                if (line != null) {
                    tokenizer = new StringTokenizer(line);
                }
                else {
                    eof = true;
                }
            }
        }
        catch (Exception e) {
        }
        
        return !eof;
    }
    
    public static String next() {
    
        try {
            while ( ! tokenizer.hasMoreTokens() ) {
                tokenizer = new StringTokenizer(reader.readLine() );
            }
            
        }
        catch (Exception e) {
        
        }
        
        return tokenizer.nextToken();
    }

    public static Integer nextInt() {
        Integer ret = null;
        
        try {
            ret = Integer.parseInt(next());
        }
        catch (Exception e) {
        }
        
        return ret;
    }

    public static int permutation(char[][] numbers, int[] prefix, HashSet<Integer> sufix) {
    
        int minimal = Integer.MAX_VALUE;
    
        if (sufix.size() > 0) {
        
            HashSet<Integer> clone = (HashSet<Integer>) sufix.clone();
            int res;
            for(int pos: clone) {
                prefix[prefix.length - clone.size()] = pos;
                sufix.remove(pos);
                
                res = permutation(numbers, prefix, sufix);
                minimal = (res < minimal) ? res : minimal;
                
                sufix.add(pos);
            }
        
        }
        else {
            char[] temp = new char[prefix.length];
            int pos;
            int number = 0;
            int higher = 0;
            int lower = Integer.MAX_VALUE;
            
            for(int i=0; i<numbers.length; i++) {
                
                pos=0;
                for(int p: prefix) {
                    temp[pos++] = numbers[i][p];
                }
                
                try {
                    number = Integer.parseInt(new String(temp));
                }
                catch(Exception e) {};
                
                higher = (number > higher)?number:higher;
                lower = (number < lower)?number:lower;
            }
            
            minimal = higher-lower;
        
        }
    
        return minimal;
    }

    public static void main (String args[]) {

        init(System.in);
        
        int n = nextInt();
        int k = nextInt();
        
        char[][] numbers = new char[n][k];
        String number;

        int pos=0;
        while (hasNext()) {
            number = next();        
            numbers[pos++] = number.toCharArray();
        }
        
        Integer[] positions = new Integer[k];
        for (int i=0; i<k; i++) {
            positions[i] =  i;
        }
        
        HashSet<Integer> positionsSet = new HashSet<Integer>(Arrays.asList(positions));
        int max = permutation(numbers, new int[k], positionsSet);
        out.println(max);
        
        out.close();
    }
}