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