require'prime' eval"M,_,*C="+$<.read.split*?, d=[-M]*M<M||d[M-j]>k||d[M-j]=k+1};M.prime?&&k>0&&r+=k;M-=1} p d.max+r