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