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