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() 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): for j in range(m): idx = bisect.bisect(d[i], d[i+1][j]) if 0 < idx and d[i+1][j] - d[i][idx-1] <= piv: dp[i+1][j] |= dp[i][idx-1] if sum(dp[-1]): r = piv else: l = piv print(r if r < lim else -1)