import Data.List main=do l<-readLn n<-readLn s<-sort.map read.words<$>getLine print$fst.last$takeWhile((<=l).snd)[(a,sum$take a s)|a<-[1..n]]