INF = 1 << 60 N, M = map(int, input().split()) D = [] for _ in range(N): D.append(sorted(map(int, input().split()))) # 距離 d 以下にできるか def can(m): xs = [True] * M for i in range(1, N): ys = [False] * M p = 0 for j in range(M): if xs[j] == 0: continue a = D[i-1][j] while p < M and D[i][p] < a: p += 1 while p < M and a <= D[i][p] <= a+m: ys[p] = True p += 1 xs = ys return any(xs) lo = 0 hi = INF ans = hi while lo <= hi: m = (lo + hi) // 2 if can(m): ans = min(ans, m) hi = m - 1 else: lo = m + 1 if ans == INF: print(-1) else: print(ans)