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 #================================================== class Product_List_2D: def __init__(self, default, H, W): """ H x W の直積リストを生成する. """ self.H=H self.W=W self.list=[default]*(H*W) def __len__(self): return self.H*self.W def get(self, i, j): if i<0: i+=self.H if j<0: j+=self.W return self.list[i*self.W+j] def get_all(self): return [self.list[i*self.W: (i+1)*self.W] for i in range(self.H)] def projection_H(self, i): if i<0: i+=self.H return self.list[i*self.W: (i+1)*self.W] def set(self, i, j, value): if i<0: i+=self.H if j<0: j+=self.W self.list[i*self.W+j]=value def set_once(self, i, A): if i<0: i+=self.H index=i*self.W for j in range(self.W): self.list[index]=A[j] index+=1 def __getitem__(self, index): return self.get(index[0], index[1]) def __setitem__(self, index, value): self.set(index[0], index[1], value) def __repr__(self): return "[Product List 2D] : Height: {}, Width: {}".format(self.H, self.W) #================================================== def solve(): N,M=map(int,input().split()) D=Product_List_2D(0,N,M) for i in range(N): Di=list(map(int,input().split())) Di.sort() D.set_once(i, Di) DP=Product_List_2D(0,N,M) DP.set_once(0,[1]*M) def check(X): for i in range(1,N): l=0; r=0; k=0 for j in range(M): while rX: k-=DP[i-1,l] l+=1 DP[i,j]=1 if k else 0 return any(DP.projection_H(-1)) if not check(float("inf")): return -1 return General_Binary_Increase_Search_Integer(0,10**9,check) #================================================== print(solve())