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 static java.lang.Math.*;
import static java.lang.Integer.*;

import java.math.BigInteger;
import java.util.Arrays;

public class B {
static class PermutationGenerator {

      public int[] a;
      public BigInteger numLeft;
      public BigInteger total;
      
      public PermutationGenerator (int n) {
            if (n < 1) {
              throw new IllegalArgumentException ("Min 1");
            }
            a = new int[n];
            total = getFactorial (n);
            reset ();
          }
      public void reset () {
            for (int i = 0; i < a.length; i++) {
              a[i] = i;
            }
            numLeft = new BigInteger (total.toString ());
        }
      public BigInteger getNumLeft () {
            return numLeft;
         }
      public BigInteger getTotal () {
            return total;
          }
      public boolean hasMore () {
            return numLeft.compareTo (BigInteger.ZERO) == 1;
          }
      public static BigInteger getFactorial (int n) {
            BigInteger fact = BigInteger.ONE;
            for (int i = n; i > 1; i--) {
              fact = fact.multiply (new BigInteger (Integer.toString (i)));
            }
            return fact;
         }
      public int[] getNext () {

            if (numLeft.equals (total)) {
              numLeft = numLeft.subtract (BigInteger.ONE);
              return a;
            }

            int temp;

            int j = a.length - 2;
            while (a[j] > a[j+1]) {
              j--;
            }

            int k = a.length - 1;
            while (a[j] > a[k]) {
              k--;
            }

            temp = a[k];
            a[k] = a[j];
            a[j] = temp;
            int r = a.length - 1;
            int s = j + 1;

            while (r > s) {
              temp = a[s];
              a[s] = a[r];
              a[r] = temp;
              r--;
              s++;
            }

            numLeft = numLeft.subtract (BigInteger.ONE);
            return a;

          }

        }
      
    public static void main(String[] args)throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String [] use = br.readLine().split(" ");
        int N = parseInt(use[0]);
        int K = parseInt(use[1]);
        String [] nums = new String [N];
        for(int i = 0 ; i < N ; ++i){
            nums[i] = br.readLine();
        }
        int [] idx = new int [K];
        for(int i = 0 ; i < idx.length ; ++i){
            idx[i] = i;
        }
        int [] indices;
        PermutationGenerator x = new PermutationGenerator(idx.length);
        int min = (int)1e9;
        while(x.hasMore()){
            indices = x.getNext();
            String [] newNums = new String [N];
            for(int i = 0 ; i < N ; ++i){
                String cur = nums[i];
                String n = "";
                for(int j = 0 ; j < indices.length ; ++j){
                    n += cur.charAt(idx[indices[j]]);
                }
                newNums[i] = n;
            }
            int [] xx = new int [N];
            for(int i = 0 ; i < newNums.length ; ++i){
                xx[i] = parseInt(newNums[i]);
            }
            Arrays.sort(xx);
            min = min(min, xx[xx.length - 1] - xx[0]);
        }
        System.out.println(min);
    }
}