eval"N,K,*A="+`dd`.split*?, x,y=0,N*K A.reduce{x,y=[x+K,y+K,[x+z=_2-_1,x].max].min,[x+K,y+K,z<0?y-z:y].min;_2} p [x,y].min