import bisect def is_ok(v): S = set(range(len(D[0]))) for i in range(N-1): ND = D[i+1] NS = set() for s in S: d = D[i][s] l = bisect.bisect_left(ND,d) r = bisect.bisect_right(ND,d+v) for j in range(l,r):#TLEするようならimos使う NS.add(j) S = NS return S def meguru_bisect(ng, ok): while (abs(ok - ng) > 1): mid = (ok + ng) // 2 if is_ok(mid): ok = mid else: ng = mid return ok N,M = map(int, input().split()) D = [] for i in range(N): D.append(sorted(list(set(map(int, input().split()))))) ans = meguru_bisect(-1, 10**9 + 100) if ans == 10**9+100: print(-1) else: print(ans)