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.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.*;

public class Permutations
{
    public static void main(String[] args)
    {
        new Permutations(System.in, System.out);
    }

    private static class Solver implements Runnable
    {
        int n, k;
        char[][] numbers;
        InputReader in;
        PrintWriter out;

        void solve() throws IOException
        {
            n = in.nextInt();
            k = in.nextInt();
            numbers = new char[n][];

            for (int i = 0; i < n; i++)
            {
                String s = "" + in.nextInt();

                while (s.length() < k)
                    s = "0" + s;

                numbers[i] = s.toCharArray();
            }

            int[] perm = new int[k];
            int min = (int) 1e9 + 7;

            for (int i = 0; i < k; i++)
                perm[i] = i;

            do
            {
                min = Math.min(min, find(perm));
            } while (Utils.nextPermutation(perm));

            out.println(min);
        }

        int find(int[] perm)
        {
            int[] x = new int[n];

            for (int i = 0; i < n; i++)
            {
                String s = "";

                for (int j = 0; j < k; j++)
                    s += numbers[i][perm[j]];

                x[i] = Integer.parseInt(s);
            }

            Arrays.sort(x);

            return x[n - 1] - x[0];
        }

        Solver(InputReader in, PrintWriter out)
        {
            this.in = in;
            this.out = out;
        }

        @Override
        public void run()
        {
            try
            {
                solve();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }

    }

    private static class InputReader
    {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;

        int read()
        {
            if (numChars == -1)
                throw new InputMismatchException();

            if (curChar >= numChars)
            {
                curChar = 0;
                try
                {
                    numChars = stream.read(buf);
                }
                catch (IOException e)
                {
                    throw new InputMismatchException();
                }
                if (numChars <= 0)
                    return -1;
            }

            return buf[curChar++];
        }

        int nextInt()
        {
            int c = read();

            while (isSpaceChar(c))
                c = read();

            int sgn = 1;

            if (c == '-')
            {
                sgn = -1;
                c = read();
            }

            int res = 0;

            do
            {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();

                res *= 10;
                res += c & 15;

                c = read();
            } while (!isSpaceChar(c));

            return res * sgn;
        }

        boolean isSpaceChar(int c)
        {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        void close()
        {
            try
            {
                stream.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }

        InputReader(InputStream stream)
        {
            this.stream = stream;
        }

    }

    private static class Utils
    {
        static boolean nextPermutation(int[] arr)
        {
            for (int a = arr.length - 2; a >= 0; --a)
            {
                if (arr[a] < arr[a + 1])
                {
                    for (int b = arr.length - 1; ; --b)
                    {
                        if (arr[b] > arr[a])
                        {
                            int t = arr[a];

                            arr[a] = arr[b];
                            arr[b] = t;

                            for (++a, b = arr.length - 1; a < b; ++a, --b)
                            {
                                t = arr[a];
                                arr[a] = arr[b];
                                arr[b] = t;
                            }

                            return true;
                        }
                    }
                }
            }

            return false;
        }

    }

    private Permutations(InputStream inputStream, OutputStream outputStream)
    {
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        Thread thread = new Thread(null, new Solver(in, out), "Solver", 1 << 29);

        try
        {
            thread.start();
            thread.join();
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }

        in.close();
        out.flush();
        out.close();
    }

}