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