Go: package main

import (
	"container/heap"
	"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)
	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)
	dx := []int64{0, 1, 0, -1}
	dy := []int64{1, 0, -1, 0}
	memo := make(map[PersonPosition]struct{}, 0)
	for pq.Len() > 0 {
		now := heap.Pop(&pq).(*Status)
		if now.Person.Distance() == 0 {
			fmt.Println(now.Turn)
			return
		}
		if _, ok := memo[now.Person]; ok {
			continue
		}
		memo[now.Person] = struct{}{}
		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)
			}
		}
	}
	fmt.Println("NA")
}

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

C++: #include <iostream>
#include <vector>
#include <queue>

using namespace std;

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

int main() {
	int x, y;
	int dx[] = {1, 0, -1, 0};
	int dy[] = {0, 1, 0, -1};
	int dx2[] = {-1, 0, 1, 0};
	int dy2[] = {0, -1, 0, 1};
	while (cin >> x >> y) {
		if (x == 0 && y == 0) {
			break;
		}
		P s1, s2;
		cin >> s1.first >> s1.second;
		cin >> s2.first >> s2.second;
		s1.first--;
		s1.second--;
		s2.first--;
		s2.second--;
		vector< vector<int> > mp(y, vector<int>(x));
		if (s1 == s2) {
			cout << 0 << endl;
		} else {
			int b;
			for (int i = 0; i < y; i++) {
				for (int j = 0; j < x; j++) {
					cin >> b;
					if (b == 1) {
						mp[i][j] = -1;
					} else {
						mp[i][j] = 0;
					}
				}
			}
			mp[s1.second][s1.first] = mp[s2.second][s2.first] = -1;
			queue<R> q;
			q.push(R(0, Q(s1, s2)));
			bool h2 = false;
			while(!q.empty()) {
				R r = q.front();
				q.pop();
				P p1 = (r.second).first;
				P p2 = (r.second).second;
				bool h = false;
				//cout << r.first+1 << endl;
				//cout << p1.first << ' ' << p1.second << endl;
				//cout << p2.first << ' ' << p2.second << endl;
				for (int i = 0; i < 4; i++) {
					if (0 <= p1.first+dx[i] && p1.first+dx[i] < x && 0 <= p1.second+dy[i] && p1.second+dy[i] < y && mp[p1.second+dy[i]][p1.first+dx[i]] == 0 &&
						0 <= p2.first+dx2[i] && p2.first+dx2[i] < x && 0 <= p2.second+dy2[i] && p2.second+dy2[i] < y && mp[p2.second+dy2[i]][p2.first+dx2[i]] == 0) {
							if ((p1.first+dx[i] == p2.first+dx2[i])&&(p1.second+dy[i] == p2.second+dy2[i])) {
								h = h2 = true;
								break;
							} else {
								R r2;
								r2.first = r.first+1;
								r2.second = Q(P(p1.first+dx[i], p1.second+dy[i]), P(p2.first+dx2[i], p2.second+dy2[i]));
								mp[p1.second+dy[i]][p1.first+dx[i]] = mp[p2.second+dy2[i]][p2.first+dx2[i]] = r2.first;
								if (r2.first >= 100) {
									h = true;
									break;
								} else {
									q.push(r2);
								}
							}
					} 
				}
				if (h) {
					if (h2) {
						cout << r.first+1 << endl;
					}
					break;
				} 
			}
			if (!h2) {
				cout << "NA" << endl;
			}
		}
	}
	return 0;
}

C++: #include<map>
#include<set>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<string>
#include<vector>
#include<complex>
#include<cstdlib>
#include<cstring>
#include<numeric>
#include<sstream>
#include<iostream>
#include<algorithm>
#include<functional>

#define mp       make_pair
#define pb       push_back
#define all(x)   (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define REP(i,b,n) for(int i=b;i<n;i++)

using namespace std;

typedef    long long          ll;
typedef    unsigned long long ull;
typedef    vector<bool>       vb;
typedef    vector<int>        vi;
typedef    vector<vb>         vvb;
typedef    vector<vi>         vvi;
typedef    pair<int,int>      pii;

const int INF=1<<29;
const double EPS=1e-9;

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

struct point{
	int ptx,pty,pkx,pky,cnt;
	point(int ptx,int pty,int pkx,int pky,int cnt):ptx(ptx),pty(pty),pkx(pkx),pky(pky),cnt(cnt){}
};

int X,Y;
int tx,ty;
int kx,ky;
int board[55][55];
bool memo[55][55][55][55];
int main(){
	while(cin>>X>>Y,X||Y){
		queue<point> que;
		while(!que.empty()){
			que.pop();
		}
		memset(memo,false,sizeof(memo));
		cin>>tx>>ty>>kx>>ky;
		for(int i = 1;i <=Y;i++){
			for(int j = 1;j <=X;j++){
				cin>>board[i][j];
			}
		}
		que.push(point(tx,ty,kx,ky,0));
		int ans = 200;
		while(!que.empty()){
			point p = que.front();
			que.pop();
			if(p.cnt>=100){
				break;
			}
			if(p.ptx==p.pkx&&p.pty==p.pky){
				ans =min(ans,p.cnt);
				break;
			}if(memo[p.ptx][p.pty][p.pkx][p.pky])
				continue;
			memo[p.ptx][p.pty][p.pkx][p.pky]=true;
			for(int i = 0;i < 4;i++){
				int ntx = p.ptx + dx[i];
				int nty = p.pty + dy[i];
				int nkx  =p.pkx + dx[(i+2)%4];
				int nky = p.pky + dy[(i+2)%4];
				if(ntx >X||ntx <=0||nty>Y||nty <=0||board[nty][ntx]==1){
					ntx =p.ptx;
					nty =p.pty;
				}
				if(nkx >X||nkx<=0||nky>Y||nky<=0||board[nky][nkx]==1){
					nkx = p.pkx;
					nky = p.pky;
				}
				que.push(point(ntx,nty,nkx,nky,p.cnt+1));
			}
		}
		if(ans>=100){
			cout <<"NA"<<endl;
		}else{
			cout <<ans<<endl;
		}
	}
	return 0;
}