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 := &Status{}
		*now = *pq[0]
		pq = pq[1:]
		if ans <= now.Turn {
			continue
		}
		if _, ok := memo[now.Person]; ok {
			continue
		}
		memo[now.Person] = now.Turn

		if now.Person.Distance() == 0 && ans > now.Turn {
			ans = 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 <algorithm>
#include <queue>

using namespace std;

class State{
public:
   int x1, y1, x2, y2;
   int cost;
};

int h, w;
bool mapData[50][50];
bool visited[50][50][50][50];
int dx[] = {0, 1, 0, -1};
int dy[] = {-1, 0, 1, 0};
int ax, ay, bx, by;
int ans;

int bfs(){
   State u, v;
   u.x1 = ax-1;
   u.y1 = ay-1;
   u.x2 = bx-1;
   u.y2 = by-1;
   u.cost = 0;
   visited[u.y1][u.x1][u.y2][u.x2] = true;
   queue<State> Q;
   Q.push(u);
   while(!Q.empty()){
      u = Q.front();Q.pop();
      if(u.x1 == u.x2 && u.y1 == u.y2) return u.cost;
      for(int i=0;i<4;i++){
         int ty1 = u.y1 + dy[i];
         int tx1 = u.x1 + dx[i];
         int ty2 = u.y2 - dy[i];
         int tx2 = u.x2 - dx[i];
         if(ty1<0) ty1 = 0;
         if(tx1<0) tx1 = 0;
         if(ty2<0) ty2 = 0;
         if(tx2<0) tx2 = 0;
         if(ty1>=h) ty1 = h-1;
         if(tx1>=w) tx1 = w-1;
         if(ty2>=h) ty2 = h-1;
         if(tx2>=w) tx2 = w-1;
         if(mapData[ty1][tx1]){
            ty1 = u.y1;
            tx1 = u.x1;
         }
         if(mapData[ty2][tx2]){
            ty2 = u.y2;
            tx2 = u.x2;
         }
         if(visited[ty1][tx1][ty2][tx2]) continue;
         visited[ty1][tx1][ty2][tx2] = true;
         v.x1 = tx1;
         v.y1 = ty1;
         v.x2 = tx2;
         v.y2 = ty2;
         v.cost = u.cost+1;
         Q.push(v);
      }
   }
   return -1;
}

main(){
   while(cin >> w >> h && (h || w)){
      cin >> ax >> ay >> bx >> by;
      fill(visited[0][0][0], visited[50][0][0], false);
      for(int i=0;i<h;i++){
         for(int j=0;j<w;j++){
            cin >> mapData[i][j];
         }
      }
      ans = bfs();
      if(ans == -1) cout << "NA" << endl;
      else cout << ans << endl;
   }
   return 0;
}

C++: #include<stdio.h>
#include<iostream>
#include<queue>
#include<string.h>
using namespace std;
struct Y{
int hx,hy,rx,ry,tim;
};
int mp[52][52],vmp[52][52];
int main(){
int n,w,h,i,j,a[2]={1,-1},b[2],g,k;
Y e;
queue<Y> q;
while(1){
	g=0;
	k=0;
	cin>>w>>h;
	if(w==0&&h==0)break;
	cin>>e.hx>>e.hy>>e.rx>>e.ry;
	e.tim=0;
	q.push(e);
	memset(mp,0,sizeof(mp));
	memset(vmp,0,sizeof(vmp));
	for(i=0;i<=h+1;i++){
		mp[i][0]=1;
		mp[i][w+1]=1;
	}
	for(i=0;i<=w+1;i++){
		mp[0][i]=1;
		mp[h+1][i]=1;
	}
	for(i=1;i<=h;i++){
		for(j=1;j<=w;j++){
			cin>>mp[j][i];
			if(mp[j][i]==1)k++;
		}
	}
	while(1){
		e=q.front();
		q.pop();
				if(vmp[e.hy][e.hx]==0)g++;
				if(vmp[e.ry][e.rx]==0)g++;
				vmp[e.hy][e.hx]=1;
				vmp[e.ry][e.rx]=2;
		if(e.tim>=100||e.hx==e.rx&&e.hy==e.ry||g==(w*h-k))break;
		e.tim++;
		for(i=0;i<2;i++){
			if(mp[e.hy+a[i]][e.hx]!=1&&vmp[e.hy+a[i]][e.hx]!=1||mp[e.ry-a[i]][e.rx]!=1&&vmp[e.ry-a[i]][e.rx]!=2){
				b[0]=0;
				b[1]=0;
				if(mp[e.hy+a[i]][e.hx]!=1){
					e.hy+=a[i];
					b[0]=1;
				}
				if(mp[e.ry-a[i]][e.rx]!=1){
					e.ry-=a[i];
					b[1]=1;
				}
				if(vmp[e.hy][e.hx]==0)g++;
				if(vmp[e.ry][e.rx]==0)g++;
				vmp[e.hy][e.hx]=1;
				vmp[e.ry][e.rx]=2;
				q.push(e);
				if(b[0]==1)e.hy-=a[i];
				if(b[1]==1)e.ry+=a[i];
			}																									
			if(mp[e.hy][e.hx+a[i]]!=1&&vmp[e.hy][e.hx+a[i]]!=1||mp[e.ry][e.rx-a[i]]!=1&&vmp[e.ry][e.rx-a[i]]!=2){
				b[0]=0;
				b[1]=0;
				if(mp[e.hy][e.hx+a[i]]!=1){
					e.hx+=a[i];
					b[0]=1;
				}
				if(mp[e.ry][e.rx-a[i]]!=1){
					e.rx-=a[i];
					b[1]=1;
				}
				if(vmp[e.hy][e.hx]==0)g++;
				if(vmp[e.ry][e.rx]==0)g++;
				vmp[e.hy][e.hx]=1;
				vmp[e.ry][e.rx]=2;
				q.push(e);
				if(b[0]==1)e.hx-=a[i];
				if(b[1]==1)e.rx+=a[i];
			}
		}
		e.tim--;
	}
	while(!q.empty())q.pop();
	if(e.tim>=100||g==(w*h-k))printf("NA\n");
	else cout<<e.tim<<endl;
}
return 0;
}

C++: #include<bits/stdc++.h>
#define inf 1<<29
using namespace std;

class State{
public:
  int a,b,c,d;
  State(int a,int b,int c,int d):a(a),b(b),c(c),d(d){};
};

int h,w;
int table[51][51],memo[51][51][51][51];
int X[4]={-1,0,1,0};
int Y[4]={0,1,0,-1};

bool in(int a,int b){
  if(a<0 || b<0 || h<=a || w<=b)return false;
  return true;
}

void init(){
  for(int i=0;i<51;i++)
    for(int j=0;j<51;j++)
      for(int k=0;k<51;k++)
	for(int l=0;l<51;l++)memo[i][j][k][l]=inf;
}

int main()
{
  int a,b,c,d;
  while(1){
    cin>>w>>h;
    if(h+w==0)break;
    cin>>b>>a>>d>>c;
    a--;b--;c--;d--;
    for(int i=0;i<h;i++)for(int j=0;j<w;j++)cin>>table[i][j];
    init();

    queue<State> q;
    q.push(State(a,b,c,d));
    memo[a][b][c][d]=0;

    int ans=inf;
    while(!q.empty()){
      State u=q.front();
      q.pop();

      if(u.a==u.c && u.b==u.d){
	ans=min(memo[u.a][u.b][u.c][u.d],ans);
	continue;
      }
      for(int i=0;i<4;i++){
	int A=u.a+Y[i],B=u.b+X[i],C=u.c+Y[(i+2)%4],D=u.d+X[(i+2)%4];
	if(!in(A,B))continue;
	if(!in(C,D))continue;
	if(table[A][B]){
	  A=u.a;
	  B=u.b;
	}
	if(table[C][D]){
	  C=u.c;
	  D=u.d;
	}
	if(memo[A][B][C][D]==inf){
	  q.push(State(A,B,C,D));
	  memo[A][B][C][D]=memo[u.a][u.b][u.c][u.d]+1;
	}
      }
    }
    if(ans==inf)cout<<"NA"<<endl;
    else cout<<ans<<endl;
  }
  return 0;
}