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
		}
		if ans <= now.Turn {
			continue
		}
		if v, ok := memo[now.Person]; ok && v <= 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)
				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 <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)continue;

		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();
}

C++: #include<bits/stdc++.h>
#define MP make_pair
#define F first
#define S second
using namespace std;
typedef pair<int, int> PII;
typedef pair<PII, PII> PPP;

int x, y;
bool visited[51][51][51][51];
bool flr[52][52];
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
int main()
{
  while(true){
    cin >> x >> y;
    if(!x && !y){
      break;
    }

    int tx, ty, kx, ky;
    memset(visited, false, sizeof visited);
    memset(flr, false, sizeof flr);
    cin >> tx >> ty >> kx >> ky;

    for(int i = 1; i <= y; i++){
      for(int j = 1; j <= x; j++){
	int in;
	cin >> in;
	flr[i][j] = (bool)in;
      }
    }

    for(int i = 1; i <= y; i++){
      flr[i][0] = flr[i][x + 1] = true;
    }
    for(int i = 1; i <= x; i++){
      flr[0][i] = flr[y + 1][i] = true;
    }

    queue<PPP> q;
    q.push(MP(MP(tx, ty), MP(kx, ky)));

    for(int i = 0; i <= 100; i++){
      int sz = q.size();
      for(int j = 0; j < sz; j++){
	PPP now = q.front();
	q.pop();
	if(!visited[now.F.F][now.F.S][now.S.F][now.S.S]){
	  visited[now.F.F][now.F.S][now.S.F][now.S.S] = true;
	  
	  if(now.F.F == now.S.F && now.F.S == now.S.S){
	    cout << i << endl;
	    goto end;
	  }
	  
	  for(int k = 0; k < 4; k++){
	    int ntdx = dx[k], ntdy = dy[k], nkdx = -dx[k], nkdy = -dy[k];
	    PPP next = MP(MP(now.F.F + ntdx * !flr[now.F.F + ntdx][now.F.S + ntdy], now.F.S + ntdy * !flr[now.F.F + ntdx][now.F.S + ntdy]), MP(now.S.F + nkdx * !flr[now.S.F + nkdx][now.S.S + nkdy], now.S.S + nkdy * !flr[now.S.F + nkdx][now.S.S + nkdy]));
	    q.push(next);
	  }
	}
      }
    }
    cout << "NA" << endl;
  end:;
  }
  return 0;
}