N,M=map(int,input().split()) A=list(map(int,input().split())) T=[list(map(int,input().split())) for _ in range(N)] ans=[-1 for _ in range(N)] ok=0 L=[0 for _ in range(M)] IDX=[0 for _ in range(N)] while not ok: ok=1 for p in range(N): if ans[p]!=-1:continue t=T[p][IDX[p]] if L[t]