import java.io.*; import java.util.*; import java.util.stream.*; public class Main { static int[][] matrix; static ArrayList> dp = new ArrayList<>(); 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(); } dp.add(new HashMap<>()); } int ans = Integer.MAX_VALUE; for (int i = 0; i < m; i++) { ans = Math.min(ans, dfw(n - 2, matrix[n - 1][i])); } if (ans >= Integer.MAX_VALUE / 2) { System.out.println(-1); } else { System.out.println(ans); } } static int dfw(int idx, int v) { if (idx < 0) { return 0; } if (!dp.get(idx).containsKey(v)) { int ans = Integer.MAX_VALUE / 2; for (int x : matrix[idx]) { if (x <= v) { ans = Math.min(ans, Math.max(dfw(idx - 1, x), v - x)); } } dp.get(idx).put(v, ans); } return dp.get(idx).get(v); } } 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(); } }