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 (pq PQStatus) Len() int {
	return len(pq)
}
func (pq PQStatus) Less(i, j int) bool {
	if pq[i].Turn == pq[j].Turn {
		return pq[i].Person.Distance() < pq[j].Person.Distance()
	}
	return pq[i].Turn < pq[j].Turn
}

func (pq PQStatus) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
}

func (pq *PQStatus) Push(x interface{}) {
	item := x.(*Status)
	*pq = append(*pq, item)
}

func (pq *PQStatus) Pop() interface{} {
	old := *pq
	n := len(old)
	item := old[n-1]
	*pq = old[0 : n-1]
	return item
}

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)
	//heap.Init(&pq)
	//heap.Push(&pq, start)
	pq.Push(start)
	dx := []int64{0, 1, 0, -1}
	dy := []int64{1, 0, -1, 0}
	memo := make(map[PersonPosition]int64, 0)
	ans := int64(101)
	for pq.Len() > 0 {
		//now := heap.Pop(&pq).(*Status)
		now := pq.Pop().(*Status)
		if now.Person.Distance() == 0 && ans > now.Turn {
			ans = now.Turn
		}
		if v, ok := memo[now.Person]; ok && v <= now.Turn {
			continue
		}
		if ans<=now.Turn {
			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 += 1
			if next.Turn < 100 {
				//heap.Push(&pq, next)
				pq.Push(next)
			}
		}
	}
	if ans == 101 {
		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>
#include <cstring>
using namespace std;

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

class State
{
public:
	int tx, ty, kx, ky;
	int time;

	State(int tx, int ty, int kx, int ky, int time) : tx(tx), ty(ty), kx(kx), ky(ky), time(time) {}
};

bool oor(int x, int y)
{
	return x<0 || x>=X || y<0 || y>=Y;
}

int main()
{
	while (cin >> X >> Y, X||Y)
	{
		int itx, ity, ikx, iky;
		cin >> itx >> ity >> ikx >> iky;

		bool map[Y][X];
		for (int y=0; y<Y; y++)
		for (int x=0; x<X; x++)
			cin >> map[y][x];

		queue<State> q;
		q.push(State(itx-1, ity-1, ikx-1, iky-1, 0));
		bool v[Y][X][Y][X];
		memset(v, 0, sizeof(v));
		bool f = 0;

		while (!q.empty())
		{
			State t = q.front(); q.pop();
			if (v[t.ty][t.tx][t.ky][t.kx]) continue;
			if (t.time >= 100)
			{
				break;
			}

			if (t.tx==t.kx && t.ty==t.ky)
			{
				cout << t.time << endl;
				f = 1;
				break;
			}
			v[t.ty][t.tx][t.ky][t.kx] = 1;

			for (int i=0; i<4; i++)
			{
				State next = State(t.tx+dx[i], t.ty+dy[i], t.kx+dx[(i+2)%4], t.ky+dy[(i+2)%4], t.time+1);
				if (oor(next.tx, next.ty) && oor(next.kx, next.ky)) continue;
				if (oor(next.tx, next.ty) || map[next.ty][next.tx])
				{
					next.tx = t.tx;
					next.ty = t.ty;
				}

				if (oor(next.kx, next.ky) || map[next.ky][next.kx])
				{
					next.kx = t.kx;
					next.ky = t.ky;
				}
				
				if (v[next.ty][next.tx][next.ky][next.kx]) continue;

				q.push(next);
			}
		}

		if (!f)
			cout << "NA" << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<queue>
#include<vector>
#include<sstream>
#include<algorithm>
using namespace std;
#define REP(i,b,n) for(int i=b;i<n;i++)
#define rep(i,n)   REP(i,0,n)
#define pb         push_back


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

bool vis[50][50][50][50];
bool m[50][50];

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

void bfs(int r,int c,int sy,int sx,int gy,int gx){
  rep(i,r)rep(j,c)rep(k,r)rep(l,c)vis[i][j][k][l]=false;
  vis[sy][sx][gy][gx]=true;
  queue<state> Q;
  Q.push((state){0,sy,sx,gy,gx});
  while(!Q.empty()){
    state now = Q.front();Q.pop();
    //    cout << now.x1 <<" " << now.y1 <<":" << now.x2 <<" " << now.y2 << endl;
    if (now.cost >= 100)break;
    if (now.x1==now.x2 && now.y1==now.y2){
      cout << now.cost << endl;
      return;
    }

    rep(i,4){
      int nex =now.x1+dx[i] ,ney =now.y1+dy[i];
      int nnex=now.x2+dx2[i],nney=now.y2+dy2[i];
      if (nex==-1 ||ney==-1 ||nex==c ||ney==r ||m[ney][nex]){
	nex=now.x1;
	ney=now.y1;
      }
      if (nnex==-1||nney==-1||nnex==c||nney==r||m[nney][nnex]){
	nnex=now.x2;
	nney=now.y2;
      }
      
      if (vis[ney][nex][nney][nnex]){
	continue;
      }
      vis[ney][nex][nney][nnex]=true;
      Q.push((state){now.cost+1,ney,nex,nney,nnex});
    }



  }
  cout <<"NA"<<endl;
}

main(){
  int c,r;
  while(cin>>c>>r&& c){
    int sy,sx,gy,gx;
    cin>>sx>>sy>>gx>>gy;
    sx--;sy--;gx--;gy--;
    rep(i,r){
      rep(j,c)cin>>m[i][j];
    }

    //rep(i,r){
    //      rep(j,c)cout <<m[i][j];
    //      cout << endl;
    //    }

    bfs(r,c,sy,sx,gy,gx);
  }
}