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