import sys input = sys.stdin.readline N, M = map(int, input().split()) D = [] for i in range(N): D.append(list(map(int, input().split()))) D[i].sort() def check(m): pre = [1] * M Dpre = D[0] for i in range(1, N): dp = [0] * M Di = D[i] now = 0 for j in range(M): while now < M: if pre[now] and Dpre[now] <= Di[j] <= Dpre[now] + m: dp[j] = 1 break now += 1 dp, pre = pre, dp Dpre, Di = Di, Dpre return sum(pre) yes = 10 ** 9 + 5 no = -1 if not check(yes): print(-1) exit() while yes - no != 1: mid = (yes + no)//2 if check(mid): yes = mid else: no = mid print(yes)