import Data.Maybe
import qualified Data.ByteString.Char8 as B
import qualified Data.IntSet as IS
import qualified Data.Vector.Unboxed as V
main=mapM_(print.solve).analyze.map(map (fst.fromJust.B.readInt).B.words).B.lines=<<B.getContents
analyze ([n,m]:cs)=(n,m,as):analyze bs where (as,bs)=splitAt n cs
analyze _=[]
solve::(Int,Int,[[Int]])->Int
solve (n,m,as)=fst$foldr f (n,(as,IS.empty)) [2..m] where
  f _ (r,(as,s)) = (min r k,(map (dropWhile (flip IS.member t)) as,t))where
    c=V.accum (+)(V.replicate (m+1) 0)$map (\a->(head a,1)) as
    j=V.maxIndex c
    k=c V.! j
    t=IS.insert j s