import sys input = lambda: sys.stdin.readline().rstrip() def main(): # 入力 N, M = map(int, input().split()) D = [sorted(list(map(int, input().split()))) for _ in range(N)] # 計算・出力 ng = -1 ok = 10**9 while abs(ng-ok) > 1: mid = (ok+ng)//2 dp = [True]*M for i in range(1, N): dp0 = [False]*M l = 0 r = 0 bool = 0 for j in range(M): while r < M and D[i-1][r] <= D[i][j]: bool += 1 if dp[r] else 0 r += 1 while l < M and D[i-1][l] < D[i][j] - mid: bool -= 1 if dp[l] else 0 l += 1 dp0[j] = (bool > 0) dp = dp0 if any(dp) : # ここ頑張る ok = mid else: ng = mid print(ok if ok != 10**9 else -1) if __name__ == "__main__": main()