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


public class b {
    BufferedReader in;
    StringTokenizer st;
    PrintWriter out;

    String _token() throws IOException {
        while (in.ready() && !st.hasMoreTokens()) {
            st = new StringTokenizer(in.readLine());
        }
        return st.nextToken();
    }

    int _int() throws NumberFormatException, IOException {
        return Integer.parseInt(_token());
    }

    long _long() throws NumberFormatException, IOException {
        return Long.parseLong(_token());
    }

    double _double() throws NumberFormatException, IOException {
        return Double.parseDouble(_token());
    }

    void deb(String s) {
        System.err.println(s);
    }

    void deb(Object a) {
        System.err.println(a.toString());
    }

    void init(boolean FILE, String name) throws NumberFormatException,
            IOException {
        Locale.setDefault(Locale.US);
        if (FILE) {
            in = new BufferedReader(new FileReader(name + ".in"));
            out = new PrintWriter(name + ".out");
        } else {
            in = new BufferedReader(new InputStreamReader(System.in));
            out = new PrintWriter(System.out);
        }
        st = new StringTokenizer("");
    }
    
    int p[], n, k;
    
    
    void swap(int a, int b) {
        int t = p[a];
        p[a] = p[b];
        p[b] = t;
    }
    
    boolean next_perm()
    {
        int i = k-1;
        
        while(i > 0 && p[i] < p[i-1])
            i--;
    
        if (i == 0 && p[i] == k-1)
            return false;
        i--;
        
        int j = i+1;
        while (j < k && p[j] > p[i])
            j++;
        j--;
        
        swap(i,j);
        Arrays.sort(p, i+1, k);
        return true;
    }

    void mainProgram() throws NumberFormatException, IOException {
        //      test("b.in");
        init(false, "b");
        
        n = _int();
        k = _int();
        p = new int[k];
        for(int i=0;i<k;i++)
            p[i] = i;
        
        char[][]s = new char[n][k];
        for(int i=0;i<n;i++)
            s[i] = _token().toCharArray();
        
        BigInteger best = val(999999999999999l);
        
        do 
        {
            BigInteger max = val(0);
            BigInteger min = val(999999999999999l);
            
            for(int i=0;i<n;i++) {
                BigInteger now = val(0);
                for(int j=0;j<k;j++) {
                    now = add(mult(now,val(10)),val(s[i][p[j]]-'0'));
                }
                max = max.max(now);
                min = min.min(now);
            }
            best = best.min(sub(max,min));
        } while (next_perm());
        out.println(best);
        out.close();
    }

    int rand(int x) {
        return (int) (Math.random() * x);
    }

    void test(String name) throws IOException {
        out = new PrintWriter(new File(name));

        out.close();
    }

    public static void main(String[] args) throws NumberFormatException,
            IOException {
        long T = System.currentTimeMillis();
        new b().mainProgram();
        System.err.println("Time of working: "
                + (System.currentTimeMillis() - T) + " ms");
    }

    BigInteger add(BigInteger A, BigInteger B) {
        return A.add(B);
    }

    BigInteger mult(BigInteger A, BigInteger B) {
        return A.multiply(B);
    }

    BigInteger sub(BigInteger A, BigInteger B) {
        return A.subtract(B);
    }

    BigInteger div(BigInteger A, BigInteger B) {
        return A.divide(B);
    }

    BigInteger mi(BigInteger A) {
        return (BigInteger.ZERO).subtract(A);
    }

    BigInteger sq(BigInteger A) {
        return A.multiply(A);
    }

    BigInteger val(long a) {
        return BigInteger.valueOf(a);
    }

    int cmp(BigInteger a, BigInteger b) {
        return a.compareTo(b);
    }

}