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)
	pq = append(pq, 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)
		now := pq[0]
		pq = pq[1:]
		if now.Person.Distance() == 0 && ans > now.Turn {
			ans = now.Turn
			break
		}
		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 += 1
			if next.Turn < 100 {
				//heap.Push(&pq, next)
				//pq.Push(next)
				pq = append(pq, 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 (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 <cstdio>
#include <cstdlib>
#include <time.h>
#include <math.h>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <iterator>
#include <sstream>
#include <string>
#include <bitset>
using namespace std;
#define FOR(I,F,N) for(int I = F; I < (int)(N); I++)
#define rep(i, n) FOR(i, 0, n)
#define FIN(V) cout<<V<<endl
#define pb push_back
#define INF (1 << 30)

template<typename T>
void remove(vector<T>& vector, unsigned int index){
vector.erase(vector.begin() + index);
}

typedef pair<int, int> P;
typedef long long ll;
typedef priority_queue<int> pq;
int StrToInt(string);
string IntToStr(int);
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};

int H, W;
//maps[y][x];
int state[55][55];
int maps[55][55][55][55];

struct Data{
	P t, k;
	int cost;
	Data(P _t, P _k, int c){
		t = _t;
		k = _k;
		cost = c;
	}Data(){}
};

int bfs(Data d){
	queue<Data> que;
	que.push(d);
	int ans = -1;
	while(que.size()){
		Data d = que.front(); que.pop();
		P taka = d.t;
		P kazu = d.k;
		int cost = d.cost;
		if(cost >= 100)break;

		if(maps[taka.first][taka.second][kazu.first][kazu.second] < cost)continue;
		maps[taka.first][taka.second][kazu.first][kazu.second] = cost;

		if(taka.first == kazu.first && taka.second == kazu.second){
			ans = cost;
			break;
		}
		for(int i = 0; i < 4; i++){
			int ntx, nty, nkx, nky;
			ntx = taka.first + dx[i];
			nty = taka.second + dy[i];
			nkx = kazu.first - dx[i];
			nky = kazu.second - dy[i];
			if(ntx <= -1 || nty <= -1){
				ntx = taka.first;
				nty = taka.second;
			}
			if(nkx <= -1 || nky <= -1){
				nkx = kazu.first;
				nky = kazu.second;
			}
			if(state[nky][nkx] != 0){
				nkx = kazu.first;
				nky = kazu.second;
			}
			if(state[nty][ntx] != 0){
				ntx = taka.first;
				nty = taka.second;
			}
			///cout << ntx << "," << nty << " " << nkx << "," << nky << endl;
			que.push(Data(P(ntx, nty), P(nkx, nky), cost+1));
		}
	}
	return ans;
}


int main(void){
	
	while(cin >> W >> H, W|H){
		int x, y;
		fill_n(*state, 55*55, -1);
		fill_n(***maps, 55*55*55*55, INF);
		cin >> x >> y;
		x--;y--;
		P taka = P(x,y);
		cin >> x >> y;
		x--;y--;
		P kazu = P(x,y);
		rep(i, H)rep(j, W){
			cin >> state[i][j];
		}
		int ans = bfs(Data(taka, kazu, 0));
		if(ans < 0 || ans >= 100){
			cout << "NA" << endl;
			continue;
		}
		cout << ans << endl;
	}

	return 0;
}



int StrToInt(string s){
	stringstream ss;
	ss << s;
	int val;
	ss >> val;
	return val;
}
string IntToStr(int i){
	stringstream ss;
	ss << i;
	return ss.str();
}