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.text.*;
    import java.util.*;
    import java.util.regex.*;

    public class Main{
        static class Run implements Runnable{
            //TODO parameters
            final boolean consoleIO = true;
            final String inFile = "input.txt";
            final String outFile = "output.txt";
            
            int n, k;
            Vector<Integer[]> v;
            Integer[] t;
            
            void swap(int a, int b) {
                Integer tmp = t[a];
                t[a] = t[b];
                t[b] = tmp;
            }
            
            void gen(int n) {
                if(n == 1)
                    v.add(t.clone());
                else {
                    for(int i = 0; i < n; ++i) {
                        swap(i, n-1);
                        gen(n-1);
                        swap(i, n-1);
                    }
                }
            }
            
            String[] dig;
            
            int get(String s, Integer[] seq) {
                StringBuilder b = new StringBuilder();
                for(int i = 0; i < seq.length; ++i)
                    b.append(s.charAt(seq[i]));
                
                return Integer.valueOf(b.toString());
            }
            
            Pair<Integer,Integer> dif(Integer[] seq) {
                int min = Integer.MAX_VALUE;
                int max = Integer.MIN_VALUE;
                
                for(int i = 0; i < dig.length; ++i) {
                    int cur = get(dig[i], seq);
                    if(cur < min)
                        min = cur;
                    
                    if(cur > max)
                        max = cur;
                }
                
                return new Pair<Integer,Integer>(min, max);
            }
            
            @Override
            public void run() {
                n = nextInt();
                k = nextInt();
                v = new Vector<Integer[]>();
                t = new Integer[k];
                
                for(int i = 0; i < k; ++i)
                    t[i] = i;
                gen(k);
                
                dig = new String[n];
                for(int i = 0; i < n; ++i)
                    dig[i] = nextLine();
                
                int sz = v.size();
                int min = Integer.MAX_VALUE;
                
                for(int i = 0; i < sz; ++i) {
                    Pair<Integer,Integer> p = dif(v.get(i)); 
                    int cur = p.b - p.a;
                    
                    if(cur < min)
                        min = cur;
                }
                
                print(min);
                close();
            }
        //=========================================================================================================================
            BufferedReader in;
            PrintWriter out;
            StringTokenizer strTok;
           
            Run() {
                if (consoleIO) {
                    initConsoleIO();
                }
                else {
                    initFileIO();
                }
            }
           
            void initConsoleIO() {
                in = new BufferedReader(new InputStreamReader(System.in));
                out = new PrintWriter(new OutputStreamWriter(System.out));
            }
           
            void initFileIO() {
                try {
                    in = new BufferedReader(new FileReader(inFile));
                    out = new PrintWriter(new FileWriter(outFile));
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
           
            void close() {
                try {
                    in.close();
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
           
            int nextInt() {
                return Integer.parseInt(nextToken());
            }
           
            double nextDouble() {
                return Double.parseDouble(nextToken());
            }
           
            float nextFloat() {
                return Float.parseFloat(nextToken());
            }
           
            long nextLong() {
                return Long.parseLong(nextToken());
            }
           
            String nextLine() {
                try {
                    return in.readLine();
                } catch (IOException e) {
                    return "__NULL";
                }
            }
           
            boolean hasMoreTokens() {
                return (strTok == null) || (strTok.hasMoreTokens());
            }
           
            String nextToken() {
                while (strTok == null || !strTok.hasMoreTokens()) {
                    String line;
                    try {
                        line = in.readLine();
                        strTok = new StringTokenizer(line);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
               
                return strTok.nextToken();
            }
           
            void cout(Object o){
                System.out.println(o);
            }
           
            void print(Object o) {
                out.write(o.toString());
            }
            
            void println(Object o) {
                out.write(o.toString() + '\n');
            }
           
            void printf(String format, Object... args) {
                out.printf(format, args);
            }
           
            String sprintf(String format, Object... args) {
            return MessageFormat.format(format, args);
        }
        }
       
        static class Pair<A, B> {
            A a;
            B b;
           
            A f() {
                return a;
            }
           
            B s() {
                return b;
            }
           
            Pair(A a, B b) {
                this.a = a;
                this.b = b;
            }
           
            Pair(Pair<A, B> p) {
                a = p.f();
                b = p.s();
            }
        }
       
        public static void main(String[] args) throws IOException {
            Run run = new Run();
            Thread thread = new Thread(run);
            thread.run();
        }
    }