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:
/**
 * Created by IntelliJ IDEA.
 * User: shakhov
 * Date: 15.06.2011
 * Time: 15:22:46
 * To change this template use File | Settings | File Templates.
 */

//6
//86 402 133 524 405 610   6 4 1

import java.io.*;
import java.util.*;

public class CodeForces {
	double eps = 0.00001;

	public void solve() throws IOException {
		int n = nextInt();
		int k = nextInt();
		char[][] nmbs = new char[n][k];
		for (int i = 0; i < n; i++) {
			nmbs[i] = nextToken().toCharArray();
		}
		int min=Integer.MAX_VALUE;
		int arr[] = new int[k];
		for (int i = 0; i < k; i++) {
			arr[i] = i;
		}

		while (true) {
			int[] tarr=new int[n];
			for (int i = 0; i < n; i++) {
				int t = 0;
				int tt = 1;
				for (int l = 0; l < k; l++) {
					t += ((int) nmbs[i][arr[l]]-'0')*tt;
					tt *= 10;
				}
				tarr[i]=t;
			}
			Arrays.sort(tarr);
			min=Math.min(min, tarr[n-1]-tarr[0]);			
			
			int c = k - 2;
			while (c > -1 && arr[c] > arr[c + 1])
				c--;
			if (c > -1) {
				int j = c + 1;
				while (j < k - 1 && arr[j + 1] > arr[c])
					j++;
				swap(c, j, arr);
				for (j = c + 1; j < (k + c+1) / 2; j++) {
					swap(j, k - j + c, arr);
				}
			} else {
				break;
			}
		}
		
		out.print(min);
	}

	private void swap(int i, int j, int[] arr) {
		int c = arr[i];
		arr[i] = arr[j];
		arr[j] = c;
	}

	private BufferedReader reader;
	private StringTokenizer tokenizer;
	private String separator;
	public PrintWriter out;

	public static void main(String[] args) throws IOException {
		// new CodeForces().run(new FileReader("input.txt"), new
		// FileWriter("output.txt"));
		new CodeForces().run(new InputStreamReader(System.in), new OutputStreamWriter(System.out));
	}

	public void run(Reader reader, Writer writer) {
		try {
			this.reader = new BufferedReader(reader);
			out = new PrintWriter(writer);
			tokenizer = new StringTokenizer("");
			separator = System.getProperty("line.separator");
			separator = Character.toString(separator.charAt(separator.length() - 1));
			long t1 = System.currentTimeMillis();
			solve();
			out.flush();
			reader.close();
			writer.close();
			long t2 = System.currentTimeMillis();
			System.err.println("Time = " + (t2 - t1));
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(1);
		}
	}

	int nextInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	long nextLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	double nextDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}

	String nextToken() throws IOException {
		while (tokenizer == null || !tokenizer.hasMoreTokens()) {
			tokenizer = new StringTokenizer(reader.readLine());
		}
		return tokenizer.nextToken();
	}

}