require'prime' m,_,*c=$<.read.split.map &:to_i d=[*-m..n=0] m.downto(1){|i|c.map{|j|d[i]<0||j>i||d[i-j]=[d[i-j],d[i]+1].max}} p d.max+Prime.each(m){|c|n+=d[c]}