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.math.*;

public class Main { 
    
    static boolean FROM_FILE = false;
    
    static class FastReader { 
        BufferedReader br; 
        StringTokenizer st; 
        public FastReader() { 
            if (FROM_FILE) {
                try {
                    br = new BufferedReader(new FileReader("input.txt")); 
                } catch (IOException error) {
                }   
            } else {
                br = new BufferedReader(new InputStreamReader(System.in));    
            }
        }
        String next() { 
            while (st == null || !st.hasMoreElements()) { 
                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()); 
        }
  
        double nextDouble() { 
            return Double.parseDouble(next()); 
        } 
        String nextLine() { 
            String str = ""; 
            try { 
                str = br.readLine(); 
            } catch (IOException e) { 
                e.printStackTrace(); 
            } 
            return str; 
        } 
    }
    
    static int n, k;
    static char[][] original;
    static int res;
    
    static void dfs(int i, char[][] current, ArrayList<Integer> choice) {
        if (i == k) {
            int min_val = Integer.MAX_VALUE, max_val = Integer.MIN_VALUE;
            for (int k = 0; k < n; k += 1) {
                int num = Integer.parseInt(String.valueOf(current[k]));
                min_val = Math.min(min_val, num);
                max_val = Math.max(max_val, num);
            }
            // System.out.println(max_val + " " + min_val);
            res = Math.min(res, max_val - min_val);
            return;
        }
        for (Integer idx : new ArrayList<Integer>(choice)) {
            choice.remove(idx);
            for (int k = 0; k < n; k += 1) current[k][i] = original[k][idx];
            dfs(i + 1, current, choice);
            choice.add(idx);
        }
    }
    
    public static void main(String[] args) { 
        FastReader fr = new FastReader(); 
        PrintWriter out = null;
        if (FROM_FILE) {
            try {
                out = new PrintWriter(new FileWriter("output.txt"));
            } catch (IOException error) {
                
            }
        } else {
            out = new PrintWriter(new OutputStreamWriter(System.out));
        }
        n = fr.nextInt(); k = fr.nextInt();
        original = new char[n][];
        for (int i = 0; i < n; i += 1) {
            original[i] = fr.nextLine().toCharArray();
        }
        res = Integer.MAX_VALUE;
        char[][] current = new char[n][k];
        ArrayList<Integer> choice = new ArrayList<>();
        for (int i = 0; i < k; i += 1) choice.add(i);
        dfs(0, current, choice);
        out.println(res);
        
        out.flush();
    } 
}