from typing import List, Tuple, Callable, TypeVar, Optional import sys import itertools import heapq import bisect import math from collections import deque, defaultdict from functools import lru_cache, cmp_to_key input = sys.stdin.readline if __file__ != 'prog.py': sys.setrecursionlimit(10 ** 6) def readints(): return map(int, input().split()) def readlist(): return list(readints()) def readstr(): return input()[:-1] N, M = readints() D = [readlist() for _ in range(N)] for i in range(N): D[i].sort() def judge(A, i, k): NA = [] for d in D[i]: l = -1 r = len(A) while r - l > 1: c = (l + r) >> 1 if A[c] <= d: l = c else: r = c if l < 0: continue if d - A[l] <= k: NA.append(d) return NA l = -1 r = 10 ** 9 + 10 while r - l > 1: c = (l + r) >> 1 A = D[0] for i in range(1, N): A = judge(A, i, c) if not A: l = c break else: r = c print(r if r < 10 ** 9 + 10 else -1)