import bisect n, m = map(int, input().split()) d = [list(map(int, input().split())) for i in range(n)] for i in range(n): d[i].sort() # print(*d, sep = "\n") lim = 1 << 30 l, r = -1, lim while r - l > 1: piv = (l + r) // 2 dp = [[0] * m for i in range(n)] dp[0] = [1] * m for i in range(n-1): idx = 0 for j in range(m): while idx < m: if d[i][idx] > d[i+1][j]: idx -= 1 break idx += 1 if idx == -1: idx = 0 continue idx = min(idx, m-1) if d[i+1][j] - d[i][idx] <= piv: dp[i+1][j] |= dp[i][idx] # print(l, r, piv) # print(*dp, sep = "\n") if sum(dp[-1]): r = piv else: l = piv print(r if r < lim else -1)