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