require'prime' m,n,*c=$<.read.split.map &:to_i 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