import sys

N,M = map(int,input().split())
D = [list(map(int,input().split())) for _ in range(N)]
for i in range(N):
    D[i].sort()
    
def calc(m):
    now = D[0].copy()
    for i in range(N - 1):
        nx = []
        tmp = D[i + 1]
        j = 0
        for a in now:
            while j < M:
                if tmp[j] < a:j += 1
                elif tmp[j] > a + m:break
                else:
                    nx.append(tmp[j])
                    j += 1
        if len(nx) == 0:return False
        else:
            now = nx
    return True

start = -1
end = 10 ** 9
if calc(end) == False:
    print(-1)
    exit()

while end - start > 1:
    mid = end + start >> 1
    if calc(mid):
        end = mid
    else:
        start = mid
print(end)