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.awt.*;
import java.io.*;
import java.math.*;
import java.util.*;
import static java.lang.Math.*;

public class BetaRound92_Div2_B implements Runnable {

    BufferedReader in;
    PrintWriter out;
    StringTokenizer tok;

    public static void main(String[] args) {
        new Thread(null, new BetaRound92_Div2_B(), "", 256 * (1L << 20)).start();
    }

    @Override
    public void run() {
        try {
            long startTime = System.currentTimeMillis();
            if (System.getProperty("ONLINE_JUDGE") != null) {
                in = new BufferedReader(new InputStreamReader(System.in));
                out = new PrintWriter(System.out);
            } else {
                in = new BufferedReader(new FileReader("input.txt"));
                out = new PrintWriter("output.txt");
            }
            Locale.setDefault(Locale.US);
            tok = new StringTokenizer("");
            solve();
            in.close();
            out.close();
            long endTime = System.currentTimeMillis();
            System.err.println("Time = " + (endTime - startTime));
            long freeMemory = Runtime.getRuntime().freeMemory();
            long totalMemory = Runtime.getRuntime().totalMemory();
            System.err.println("Memory = " + ((totalMemory - freeMemory) >> 10));
        } catch (Throwable t) {
            t.printStackTrace(System.err);
            System.exit(-1);
        }
    }

    String readString() throws IOException {
        while (!tok.hasMoreTokens()) {
            tok = new StringTokenizer(in.readLine());
        }
        return tok.nextToken();
    }

    int readInt() throws IOException {
        return Integer.parseInt(readString());
    }

    long readLong() throws IOException {
        return Long.parseLong(readString());
    }

    double readDouble() throws IOException {
        return Double.parseDouble(readString());
    }

    /** http://pastebin.com/j0xdUjDn */
    static class Utils {

        private Utils() {}

        public static void mergeSort(int[] a) {
            mergeSort(a, 0, a.length - 1);
        }

        private static final int MAGIC_VALUE = 50;

        private static void mergeSort(int[] a, int leftIndex, int rightIndex) {
            if (leftIndex < rightIndex) {
                if (rightIndex - leftIndex <= MAGIC_VALUE) {
                    insertionSort(a, leftIndex, rightIndex);
                } else {
                    int middleIndex = (leftIndex + rightIndex) / 2;
                    mergeSort(a, leftIndex, middleIndex);
                    mergeSort(a, middleIndex + 1, rightIndex);
                    merge(a, leftIndex, middleIndex, rightIndex);
                }
            }
        }

        private static void merge(int[] a, int leftIndex, int middleIndex, int rightIndex) {
            int length1 = middleIndex - leftIndex + 1;
            int length2 = rightIndex - middleIndex;
            int[] leftArray = new int[length1];
            int[] rightArray = new int[length2];
            System.arraycopy(a, leftIndex, leftArray, 0, length1);
            System.arraycopy(a, middleIndex + 1, rightArray, 0, length2);
            for (int k = leftIndex, i = 0, j = 0; k <= rightIndex; k++) {
                if (i == length1) {
                    a[k] = rightArray[j++];
                } else if (j == length2) {
                    a[k] = leftArray[i++];
                } else {
                    a[k] = leftArray[i] <= rightArray[j] ? leftArray[i++] : rightArray[j++];
                }
            }
        }

        private static void insertionSort(int[] a, int leftIndex, int rightIndex) {
            for (int i = leftIndex + 1; i <= rightIndex; i++) {
                int current = a[i];
                int j = i - 1;
                while (j >= leftIndex && a[j] > current) {
                    a[j + 1] = a[j];
                    j--;
                }
                a[j + 1] = current;
            }
        }

    }

    // solution
    
    int n, k;
    int[] p;
    int[][] a;
    int best = Integer.MAX_VALUE;
    int[] rp;
    
    boolean less(int[] a, int[] b) {
        for (int i = 0; i < k; i++) {
            if (a[rp[i]] < b[rp[i]]) return true;
            if (a[rp[i]] > b[rp[i]]) return false;
        }
        return false;
    }
    
    boolean greater(int[] a, int[] b) {
        for (int i = 0; i < k; i++) {
            if (a[rp[i]] > b[rp[i]]) return true;
            if (a[rp[i]] < b[rp[i]]) return false;
        }
        return false;
    }
    
    int get(int[] a) {
        int res = 0;
        int p = 1;
        for (int i = 0; i < k; i++) {
            res += p * a[rp[k - i - 1]];
            p *= 10;
        }
        return res;
    }
    
    void update() {
        int minI = -1, maxI = -1;
        for (int i = 0; i < n; i++) {
            if (minI == -1 || less(a[i], a[minI])) {
                minI = i;
            }
            if (maxI == -1 || greater(a[i], a[maxI])) {
                maxI = i;
            }
        }
        int x = get(a[minI]), y = get(a[maxI]);
        best = min(best, abs(x - y));
    }
    
    void swap(int i, int j) {
        int t = p[i];
        p[i] = p[j];
        p[j] = t;
    }
    
    void gen(int L, int R) {
        if (L == R) {
            for (int i = 0; i < k; i++) {
                rp[p[i]] = i;
            }
            update();
        } else {
            for (int i = L; i <= R; i++) {
                swap(i, L);
                gen(L + 1, R);
                swap(i, L);
            }
        }
    }
    
    void solve() throws IOException {
        n = readInt();
        k = readInt();
        p = new int[k];
        a = new int[n][k];
        for (int i = 0; i < n; i++) {
            String s = readString();
            for (int j = 0; j < k; j++) {
                a[i][j] = s.charAt(j) - '0';
            }
        }
        for (int i = 0; i < k; i++) {
            p[i] = i;
        }
        rp = new int[k];
        gen(0, k-1);
        out.print(best);
    }

}