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