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.*;
import java.io.*;
public class pt{ 
 
          static int min=999999999;
          static void permute(int a[],int pos,int n,int num[][]){

                           if(pos==(n-1)){
                                     int arr[] = new int[num.length];
                                     for(int i=0;i<num.length;i++){
                                         int x=0;
                                        for(int j=0;j<n;j++)
                                               x = x*10+num[i][a[j]];
                                        arr[i]=x;         
                                     }
                                  Arrays.sort(arr);
                                  if(min>Math.abs(arr[0]-arr[num.length-1]))
                                     min = Math.abs(arr[0]-arr[num.length-1]);
                                  return;
                            }
                          for(int i=pos;i<n;i++){
                               int t = a[pos];
                                  a[pos]=a[i];
                                  a[i]=t;
                                permute(a,pos+1,n,num);
                                  t = a[pos];
                                  a[pos]=a[i];
                                  a[i]=t;
                         }
                   }

          public static void main(String[] args){ 
          PrintWriter out = new PrintWriter(System.out);

              int n = ni();             
              int k = ni();
         
              int num[][] = new int[n][k];
              int a[] = new int[k];

                for(int i=0;i<n;i++){
                     int t = ni();
                     int x = k-1;
                     while(t>0){
                            num[i][x--]=t%10;
                            t = t/10;
                    }
               }
               for(int i=0;i<k;i++)
                   a[i]=i; 
                permute(a,0,k,num);
               out.println(min);
               out.flush();
    } 

       static FastReader sc=new FastReader(); 

      static int ni(){
                 int x = sc.nextInt();
                 return(x);
        }
      static long nl(){
              long x = sc.nextLong();
              return(x);
         }
      static String n(){
                 String str = sc.next();
                     return(str);
       }
     static String ns(){
                 String str = sc.nextLine();
                   return(str);
      }
     static double nd(){
               double d = sc.nextDouble();
                 return(d);
       }
  static class FastReader 
    { 
        BufferedReader br; 
        StringTokenizer st; 
  
        public FastReader() 
        { 
            br = new BufferedReader(new
                     InputStreamReader(System.in)); 
        } 
  
        String next() 
        { 
            while (st == null || !st.hasMoreElements()) 
            { 
                try
                { 
                    st = new StringTokenizer(br.readLine()); 
                } 
                catch (IOException  e) 
                { 
                    e.printStackTrace(); 
                } 
            } 
            return st.nextToken(); 
        } 
  
        int nextInt() 
        { 
            return Integer.parseInt(next()); 
        } 
  
        long nextLong() 
        { 
            return Long.parseLong(next()); 
        } 
  
        double nextDouble() 
        { 
            return Double.parseDouble(next()); 
        } 
  
        String nextLine() 
        { 
            String str = ""; 
            try
            { 
                str = br.readLine(); 
            } 
            catch (IOException e) 
            { 
                e.printStackTrace(); 
            } 
            return str; 
        } 
    } 
}