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.List;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.FileReader;
import java.io.BufferedWriter;
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.File;
import java.io.Writer;
import java.util.StringTokenizer;
import java.util.Collection;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author unnamed
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		OutputWriter out = new OutputWriter(outputStream);
		TaskB solver = new TaskB();
		solver.solve(1, in, out);
		out.close();
	}
}

class TaskB {
    public void solve(int testNumber, InputReader in, OutputWriter out) {
        int n = in.nextInt(), k = in.nextInt();
        K = k;
        int[] ar = IOUtils.readIntArray(in, n);
        List<List<Integer>> perm = permutations(k);
        int dif = Integer.MAX_VALUE;
        for (List<Integer> p : perm) {
            int[] ar2 = new int[ar.length];
            for (int j = 0; j < ar.length; ++j)
                ar2[j] = shuffle(ar[j], p);
            int min = ArrayUtils.minArray(ar2);
            int max = ArrayUtils.maxAbsArray(ar2);
            dif = Math.min(dif, max - min);
        }
        out.print(dif);
    }

    public int K;

    public int shuffle(int n, List<Integer> permutation) {
        String s = String.valueOf(n);
        int l = s.length();
        if (l < K)
            for (int i = 0; i < K - l; ++i)
                s = "0" + s;
        if (n == 0)
            s += "0";
        String ans = "";
        for (int i = 0; i < permutation.size(); ++i)
            ans += String.valueOf(s.charAt(permutation.get(i) - 1));
        return Integer.parseInt(ans);
    }

    public List<List<Integer>> permutations(int n) {
        if (n == 1) {
            List<List<Integer>> ans = new ArrayList<List<Integer>>();
            ans.add(new ArrayList<Integer>());
            ans.get(0).add(1);
            return ans;
        } else {
            List<List<Integer>> rec = permutations(n - 1);
            List<List<Integer>> ans = new ArrayList<List<Integer>>();
            for (List<Integer> p : rec) {
                for (int i = 0; i < n; ++i) {
                    List<Integer> newp = new ArrayList<Integer>();
                    for (int k : p)
                        newp.add(k);
                    newp.add(i, n);
                    ans.add(newp);
                }
            }
            return ans;
        }
    }
}

class InputReader {
    BufferedReader br;
    StringTokenizer st;

    public InputReader(File f) {
        try {
            br = new BufferedReader(new FileReader(f));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public InputReader(InputStream f) {
        br = new BufferedReader(new InputStreamReader(f));
    }

    public String next() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    public int nextInt() {
        return Integer.parseInt(next());
    }

    }

class OutputWriter {
    private final PrintWriter writer;

    public OutputWriter(OutputStream outputStream) {
        writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
    }

    public OutputWriter(Writer writer) {
        this.writer = new PrintWriter(writer);
    }

    public void print(Object... objects) {
        for (int i = 0; i < objects.length; i++) {
            if (i != 0)
                writer.print(' ');
            writer.print(objects[i]);
        }
    }

    public void close() {
        writer.close();
    }
}

class IOUtils {

    public static int[] readIntArray(InputReader in, int size) {
        int[] array = new int[size];
        for (int i = 0; i < size; i++)
            array[i] = in.nextInt();
        return array;
    }

    }

class ArrayUtils {

    public static int minArray(int... array) {
        int ans = array[0];
        for (int i = 1; i < array.length; ++i)
            if (array[i] < ans) ans = array[i];
        return ans;
    }

    public static int maxAbsArray(int... array) {
        int ans = Math.abs(array[0]);
        for (int i = 1; i < array.length; ++i)
            if (Math.abs(array[i]) > ans) ans = Math.abs(array[i]);
        return ans;
    }

    }