import java.io.*; import java.util.*; import java.util.stream.*; public class Main { static int[][] matrix; public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int n = sc.nextInt(); int m = sc.nextInt(); matrix = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { matrix[i][j] = sc.nextInt(); } Arrays.sort(matrix[i]); } int left = -1; int right = Integer.MAX_VALUE / 2; while (right - left > 1) { int mm = (left + right) / 2; if (check(mm, n - 1)) { right = mm; } else { left = mm; } } if (right >= 1000000000) { System.out.println(-1); } else { System.out.println(right); } } static boolean check(int x, int idx) { boolean ans = false; for (int y : matrix[idx]) { if (check(idx - 1, y, x)) { return true; } } return false; } static boolean check(int idx, int x, int diff) { if (idx < 0) { return true; } for (int y : matrix[idx]) { if (y > x) { return false; } if (y + diff >= x) { if (check(idx -1, y, diff)) { return true; } } } return false; } } class Utilities { static String arrayToLineString(int[] arr) { return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new)); } } class Scanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); StringBuilder sb = new StringBuilder(); public Scanner() throws Exception { } public int nextInt() throws Exception { return Integer.parseInt(next()); } public long nextLong() throws Exception { return Long.parseLong(next()); } public double nextDouble() throws Exception { return Double.parseDouble(next()); } public int[] nextIntArray() throws Exception { return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray(); } public String next() throws Exception { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }