eval"N,V,X,Y,*Z="+gets.split*?, L=$<.map{|s|s.split.map(&:to_i)+M=[9e9]}+[M*N] v={[X,Y]=>V} q=v.dup i=0 (i+=1 z={} q.map{|(x,y),h|[[x+1,y],[x-1,y],[x,y+1],[x,y-1]].map{|a,b|Z==x ?(p i;exit):z[x]=v[x]=y if(y=h-L[b-1][a-1])>0&&y>(v[x=[a,b]]||-1)}} q=z)until q.empty? p -1