require'prime' eval'm,n,*c='+$<.read.split*?, d=[-m]*m<m||d[m-j]>k||d[m-j]=k+1};m.prime?&&k>0&&r+=k;m-=1}.max+r