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 codeforces {
  public static class FastIO {
    InputStream dis;
    byte[] buffer = new byte[1 << 17];
    int pointer = 0;
    public FastIO(String fileName) throws Exception {
      dis = new FileInputStream(fileName);
    }
    public FastIO(InputStream is) throws Exception {
      dis = is;
    }
    public int nextInt() throws Exception {
      int ret = 0;
      byte b;
      do {
          b = nextByte();
      } while (b <= ' ');
      boolean negative = false;
      if (b == '-') {
          negative = true;
          b = nextByte();
      }
      while (b >= '0' && b <= '9') {
          ret = 10 * ret + b - '0';
          b = nextByte();
      }
      return (negative) ? -ret : ret;
    }
    public long nextLong() throws Exception {
      long ret = 0;
      byte b;
      do {
          b = nextByte();
      } while (b <= ' ');
      boolean negative = false;
      if (b == '-') {
          negative = true;
          b = nextByte();
      }
      while (b >= '0' && b <= '9') {
          ret = 10 * ret + b - '0';
          b = nextByte();
      }
      return (negative) ? -ret : ret;
    }
    public byte nextByte() throws Exception {
      if (pointer == buffer.length) {
          dis.read(buffer, 0, buffer.length);
          pointer = 0;
      }
      return buffer[pointer++];
    }
    public String next() throws Exception {
      StringBuffer ret = new StringBuffer();
      byte b;
      do {
          b = nextByte();
      } while (b <= ' ');
      while (b > ' ') {
          ret.appendCodePoint(b);
          b = nextByte();
      }
      return ret.toString();
    }
  } 
    

  static int n, k, ans = Integer.MAX_VALUE;
  static String[] nums;
  public static void main(String[] args) throws Exception {
    FastIO r = new FastIO(System.in);
    n = r.nextInt(); k = r.nextInt();
    ArrayList<Integer> notChosen = new ArrayList<Integer>();
    nums = new String[n];
    for(int i = 0; i < n; i++){
      nums[i] = r.next();
      
    }
    for(int i = 0; i < k; i++){
      notChosen.add(i);
    }
    perms(new ArrayList<Integer>(), notChosen);



    System.out.println(ans);
  }
  public static void perms(ArrayList<Integer> perm, ArrayList<Integer> notChosen){
    if(notChosen.size() == 0){
      int max = 0;
      int min = Integer.MAX_VALUE;
      for(int i = 0; i < n; i++){
        char[] s = nums[i].toCharArray();
        char[] fin = nums[i].toCharArray();
        
        for(int j = 0; j < k; j++){
          fin[j] = s[perm.get(j)];
        }
        max = Math.max(Integer.parseInt(new String(fin)), max);
        min = Math.min(Integer.parseInt(new String(fin)), min);

      }
      ans = Math.min(ans, (max - min));
  }
  else{
    for(int i = 0; i < notChosen.size(); i++){
      perm.add(notChosen.get(i));
      notChosen.remove(i);
      perms(perm, notChosen);
      notChosen.add(i, perm.get(perm.size() - 1));
      perm.remove(perm.size() - 1);
    }
  }
}
}