def g;gets.split.map &:to_i;end
while(x,y=g;x>0)
a=g
b=g
puts a==b ?0:(
h={a+b=>0}
q=[a+b<<0]
m=(1..y).map{g}
l=[[0,1],[1,0],[0,-1],[-1,0]]
1 while(*a,e=q.shift;e&&(0..3).all?{|i|
  t,u,v,w=a.zip(l[i]+l[i-2]).map{|j,k|[j,j+k]}
  c,d=[[t,u],[v,w]].map{|(o,j),(k,n)|j>0&&j<=y&&n>0&&n<=x&&m[j-1][n-1]==0?[j,n]:[o,k]}
  c!=d&&(h[c+d]||=(e<99&&q<<c+d+[e+1];e+1))
})
e ?e+1:"NA"
)
end