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 now.Person.Distance() == 0 && ans > now.Turn {
			ans = now.Turn
		}
		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++
			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 <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef pair<ll, P> P3;
typedef pair<P ,P> PP;
constexpr ll MOD = ll(1e9) + 7;
constexpr int IINF = INT_MAX;
constexpr ll LLINF = LLONG_MAX;
constexpr int MAX_N = int(1e5) + 5;
constexpr double EPS = 1e-9;
constexpr int di[] = {0, 1, 0, -1}, dj[] = {1, 0, -1, 0};
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i > 0; i--)
#define SORT(v) sort((v).begin(), (v).end())
#define SORTR(v) sort((v).rbegin(), (v).rend())
#define ALL(v) (v).begin(), (v).end()

int w, h, ai, aj, bi, bj;
int d[55][55], memo[55][55][55][55];
bool visited[55][55][55][55];

bool isOK(int i, int j){
    return i>=0 && i<h && j>=0 && j<w && d[i][j]==0;
}

int bfs(){
    for(int i0=0;i0<h;i0++)for(int j0=0;j0<w;j0++){
        for(int i1=0;i1<h;i1++)for(int j1=0;j1<w;j1++){
            memo[i0][j0][i1][j1] = IINF/3;
            visited[i0][j0][i1][j1] = false;
        }
    }
    queue<PP> que;
    que.push({{ai, aj}, {bi, bj}});
    visited[ai][aj][bi][bj] = true;
    memo[ai][aj][bi][bj] = 0;
    while(!que.empty()){
        P p = que.front().first, q = que.front().second;
        que.pop();
        int pi, pj, qi, qj;
        pi = p.first; pj = p.second;
        qi = q.first; qj = q.second;
        if(pi == qi && pj == qj) return memo[pi][pj][qi][qj];
        for(int k=0;k<4;k++){
            int ri = pi + di[k], rj = pj + dj[k];
            int si = qi - di[k], sj = qj - dj[k];
            if(!isOK(ri, rj)) ri = pi, rj = pj;
            if(!isOK(si, sj)) si = qi, sj = qj;
            if(visited[ri][rj][si][sj]) continue;
            visited[ri][rj][si][sj] = true;
            memo[ri][rj][si][sj] = memo[pi][pj][qi][qj] + 1;
            que.push({{ri, rj}, {si, sj}});
        }
    }

    return IINF/3;
}

int main() {
    while(cin >> w >> h, w){
        cin >> aj >> ai >> bj >> bi;
        ai--; aj--; bi--; bj--;
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++) cin >> d[i][j];
        }

        int ans = bfs();
        if(ans < 100) cout << ans << endl;
        else cout << "NA" << endl;
    }
    return 0;
}

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