def General_Binary_Increase_Search_Integer(L, R, cond, default=None): """ 条件式が単調増加であるとき, 整数上で二部探索を行う. L: 解の下限 R: 解の上限 cond: 条件(1変数関数, 広義単調増加を満たす) default: Lで条件を満たさないときの返り値 """ if not(cond(R)): return default if cond(L): return L R+=1 while R-L>1: C=L+(R-L)//2 if cond(C): R=C else: L=C return R #================================================== def solve(): N,M=map(int,input().split()) D=[] for i in range(N): Di=list(map(int,input().split())) Di.sort() D.append(Di) DP=[[0]*M for _ in range(N)] DP[0]=[1]*M def check(X): for i in range(1,N): Di=D[i]; Dii=D[i-1] DPi=DP[i]; DPii=DP[i-1] l=0; r=0; k=0 for j in range(M): while rX: k-=DPii[l] l+=1 DPi[j]=1 if k else 0 return any(DP[-1]) if not check(float("inf")): return -1 return General_Binary_Increase_Search_Integer(0,10**9,check) #================================================== print(solve())