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