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