Go: package main

import (
	"fmt"
	"math"
)

type Position struct {
	X int64
	Y int64
}

func (one Position) Distance(another Position) int64 {
	return int64(math.Abs(float64(one.X-another.X)) + math.Abs(float64(one.Y-another.Y)))
}

type PersonPosition struct {
	Takayuki Position
	Kazuyuki Position
}

func (one PersonPosition) Distance() int64 {
	return one.Takayuki.Distance(one.Kazuyuki)
}

type Status struct {
	Person PersonPosition
	Turn   int64
}

type PQStatus []*Status

func solve(h, w int64) {
	var tx, ty, kx, ky int64
	fmt.Scan(&tx, &ty, &kx, &ky)
	tx--
	ty--
	kx--
	ky--
	m := make([][]int64, h)
	for i := range m {
		m[i] = make([]int64, w)
		for j := int64(0); j < w; j++ {
			fmt.Scan(&m[i][j])
		}
	}
	start := &Status{Person: PersonPosition{Takayuki: Position{X: tx, Y: ty}, Kazuyuki: Position{X: kx, Y: ky}}, Turn: 0}
	pq := make(PQStatus, 0)
	pq = append(pq, start)
	dx := []int64{0, 1, 0, -1}
	dy := []int64{1, 0, -1, 0}
	memo := make(map[PersonPosition]int64, 0)
	ans := int64(102)
	for len(pq) > 0 {
		now := pq[0]
		pq = pq[1:]
		if now.Person.Distance() == 0 && ans > now.Turn {
			ans = now.Turn
		}
		if ans <= now.Turn {
			continue
		}

		if _, ok := memo[now.Person]; ok {
			continue
		}
		memo[now.Person] = now.Turn

		for i := 0; i < 4; i++ {
			next := &Status{}
			*next = *now
			next.Person.Kazuyuki.X += dx[i]
			next.Person.Kazuyuki.Y += dy[i]
			if next.Person.Kazuyuki.X < 0 || next.Person.Kazuyuki.X >= w ||
				next.Person.Kazuyuki.Y < 0 || next.Person.Kazuyuki.Y >= h ||
				m[next.Person.Kazuyuki.Y][next.Person.Kazuyuki.X] == 1 {
				next.Person.Kazuyuki.X -= dx[i]
				next.Person.Kazuyuki.Y -= dy[i]
			}
			next.Person.Takayuki.X -= dx[i]
			next.Person.Takayuki.Y -= dy[i]
			if next.Person.Takayuki.X < 0 || next.Person.Takayuki.X >= w ||
				next.Person.Takayuki.Y < 0 || next.Person.Takayuki.Y >= h ||
				m[next.Person.Takayuki.Y][next.Person.Takayuki.X] == 1 {
				next.Person.Takayuki.X += dx[i]
				next.Person.Takayuki.Y += dy[i]
			}
			next.Turn++
			if next.Turn <= 100 {
				pq = append(pq, next)
			}
		}
	}
	if ans == 102 {
		fmt.Println("NA")
	} else {
		fmt.Println(ans)
	}
}

func main() {
	var h, w int64
	for {
		fmt.Scan(&h, &w)
		if h == 0 && w == 0 {
			break
		}
		solve(h, w)
	}
}

C++: #include<iostream>
#include<queue>
using namespace std;
struct xy{
  int tx,ty;
  int kx,ky;
  int ti;
};
int x,y;
xy tk;
int m[51][51],vt[51][51]={},vk[51][51]={};
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};

bool g(int a,int b){
  if(a<1 || a>x || b<1 || b>y) return 0;
  return 1;
}

int bfs(){
  queue<xy> q;
  xy t,tmp;
  int ta,tb,ka,kb,ans=10000,tim,cnt=0;
  q.push(tk);
  while(!q.empty()){
    t=q.front(),q.pop();
        for(int i=0;i<4;i++){
      ta=t.tx+dx[i],tb=t.ty+dy[i],ka=t.kx-dx[i],kb=t.ky-dy[i],tim=t.ti+1;
      if(g(ta,tb) || g(ka,kb)){
	if(vt[ta][tb]==2 && vk[ka][kb]==2)continue;
	if(m[ta][tb]==1 && m[ka][kb]==1) continue;
	if(m[ta][tb]==1) tmp.tx=t.tx,tmp.ty=t.ty;
	else if(vt[ta][tb]==0 && m[ta][tb]!=1)tmp.tx=ta,tmp.ty=tb,vt[ta][tb]=2;
	
	if(m[ka][kb]==1) tmp.kx=t.kx,tmp.ky=t.ky;
	else if(vk[ka][kb]==0 && m[ka][kb]!=1)tmp.kx=ka,tmp.ky=kb,vt[ta][tb]=2;
	tmp.ti=tim;
	cnt++;
	if(tmp.ti>=100  || cnt>=100)  break;
	if(tmp.tx==tmp.kx && tmp.ky==tmp.ty){
	  if(ans>tmp.ti) ans=tmp.ti;
	  continue;
	}
	q.push(tmp);
      }
    }
  }
  return ans;
}

int main(){
  while(1){
    int ans;
    cin>>x>>y;
    if(x+y==0) break;
    cin>>tk.tx>>tk.ty>>tk.kx>>tk.ky;
    for(int i=1;i<=x;i++)
      for(int j=1;j<=y;j++) vt[i][j]=0,vk[i][j]=0;
    tk.ti=0;
    for(int i=1;i<=x;i++)
      for(int j=1;j<=y;j++)  cin>>m[i][j];
    ans=bfs();
    if(ans<100) cout<<ans<<endl;
    else cout<<"NA"<<endl;
  }
  
  return 0;
}

C++: #include<iostream>
#include<string>
#include<map>
#include<queue>

#define rep(i,n) for(int i=0;i<int(n);i++)
#define sint(i) scanf("%d",&i);
#define sintt(i,j) scanf("%d%d",&i,&j);
using namespace std;

int xx,yy;

class Q{
	public:
	int bx,by,bbx,bby;
	int dep;
	Q(int bx,int by,int bbx,int bby,int dep):
	bx(bx),by(by),bbx(bbx),bby(bby),dep(dep){}
};

int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};

int masu[51][51];
int memo[51][51][51][51];

int search(int bx,int by,int bbx,int bby){
	
	queue<Q> hako;
	
	hako.push(Q(bx,by,bbx,bby,0));
	
	while(1){
		if(hako.empty()==1){
			break;
		}
		
		Q u=hako.front();
		hako.pop();
		
		if(u.dep>=100){
			break;
		}
		
		if(u.bx<=0 || u.by<=0 || u.bx>xx || u.by>yy ){
			continue;
		}
		if(u.bbx<=0 || u.bby<=0 || u.bbx>xx || u.bby>yy){
			continue;
		}
		if(masu[u.bx][u.by]==1 || masu[u.bbx][u.bby]==1){
			//puts("fsdafasdf");
			continue;
		}
		
		
		if(memo[u.bx][u.by][u.bbx][u.bby]==1){
			continue;
		}else{
			memo[u.bx][u.by][u.bbx][u.bby]=1;
		}
		
		
		/*
		printf("\n%d\n",u.dep);
		rep(i,yy){
			rep(j,xx){
				if(i+1==u.by && j+1==u.bx){
					printf("4 ");
					continue;
				}
				if(i+1==u.bby && j+1==u.bbx){
					printf("5 ");
					continue;
				}
				printf("%d ",masu[j+1][i+1]);
			}puts("");
		}
		*/
		if(u.bx==u.bbx && u.by==u.bby){
			return u.dep;
		}
		
		
		rep(i,4){
			int ddx=dx[i],ddy=dy[i];
			int mx,my;
			mx=u.bx+ddx;
			my=u.by+ddy;
			//printf("mx %d  my %d   masu %d\n",mx,my,masu[mx][my]);
			if(mx<=0 || my<=0 || mx>xx || my>yy||masu[mx][my]==1){ddx=ddy=0;}
			
			int ddxx=dx[(i+2)%4],ddyy=dy[(i+2)%4];
			mx=u.bbx+ddxx;
			my=u.bby+ddyy;
			//printf("mmx %d  mmy %d   masu %d\n",mx,my,masu[mx][my]);
			if(mx<=0 || my<=0 || mx>xx || my>yy||masu[mx][my]==1){ddxx=ddyy=0;}
			
			//printf("pushhhhhhhhhhhhhhhh %d %d %d %d\n",u.bx+ddx,u.by+ddy,u.bbx+ddxx,u.bby+ddyy);
			hako.push(Q(u.bx+ddx,u.by+ddy,u.bbx+ddxx,u.bby+ddyy,u.dep+1));
		}
	}
	return -1;
}

int main(){
	while(1){
		sintt(xx,yy);
		
		if(xx==0){
			break;
		}
		int bx,by,bbx,bby;
		sintt(bx,by);
		sintt(bbx,bby);
		
		rep(i,51)rep(j,51)rep(k,51)rep(p,51){
		memo[i][j][k][p]=0;
		}
		
		rep(i,yy){
			rep(j,xx){
				sint(masu[j+1][i+1]);
			}
		}
		
		int ans=search(bx,by,bbx,bby);
		if(ans==-1){
			puts("NA");
		}else{
			printf("%d\n",ans);
		}
	}
}

/*
3 4
1 3
2 4
0 0 0
0 1 0
0 1 0
1 0 0

3 2
1 1
2 2
0 0 0
1 0 0
*/

C++: #include <iostream>
#include <utility>
#include <queue>
#include <map>
using namespace std;

#define C(A,B) (0<=(A)&&(A)<(B))

typedef pair<pair<int,int>,pair<int,int> > P;

int main() {
	int X,Y;
	int tx,ty;
	int kx,ky;
	int d[50][50];
	int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};

	while(true) {
		queue<P> que;
		map<P,int> mp;
		cin>>X>>Y;
		if(X==0&&Y==0) break;
		cin>>tx>>ty;
		cin>>kx>>ky;
		tx--;ty--;kx--;ky--;
		for(int j=0;j<Y;j++) {
			for(int i=0;i<X;i++) {
				cin>>d[i][j];
			}
		}
		P p=P(pair<int,int>(tx,ty),pair<int,int>(kx,ky));
		mp[p]=0;
		que.push(p);
		int ans=-1;
		while(que.size()) {
			P p=que.front();
			int tx=p.first.first;
			int ty=p.first.second;
			int kx=p.second.first;
			int ky=p.second.second;
			if(tx==kx&&ty==ky) {ans=mp[p];break;}
			if(mp[p]>99) break;
			for(int i=0;i<4;i++) {
				int ntx=tx,nty=ty,nkx=kx,nky=ky;
				if(C(tx+dx[i],X)&&C(ty+dy[i],Y)&&!d[tx+dx[i]][ty+dy[i]]) {
					ntx+=dx[i],nty+=dy[i];
				}
				if(C(kx-dx[i],X)&&C(ky-dy[i],Y)&&!d[kx-dx[i]][kx-dy[i]]) {
					nkx-=dx[i],nky-=dy[i];
				}
				P p2(pair<int,int>(ntx,nty),pair<int,int>(nkx,nky));
				mp[p2]=mp[p]+1;
				que.push(p2);
			}
			que.pop();
		}
		if(ans!=-1) {
			cout<<ans<<endl;
		}else {
			cout<<"NA"<<endl;
		}
	}
	return 0;
}