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|d[c]>0&&n+=d[c]} p d.max+n