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 := 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<iostream>
#include<string>
#include<map>
#include<queue>

#define rep(i,n) for(int i=0;i<int(n);i++)
#define sint(i) scanf("%d",&i);
#define sintt(i,j) scanf("%d%d",&i,&j);
using namespace std;

int xx,yy;

class Q{
	public:
	int bx,by,bbx,bby;
	int dep;
	Q(int bx,int by,int bbx,int bby,int dep):
	bx(bx),by(by),bbx(bbx),bby(bby),dep(dep){}
};

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

int masu[51][51];
int memo[51][51][51][51];

int search(int bx,int by,int bbx,int bby){
	
	queue<Q> hako;
	
	hako.push(Q(bx,by,bbx,bby,0));
	
	while(1){
		if(hako.empty()==1){
			break;
		}
		
		Q u=hako.front();
		hako.pop();
		
		if(u.dep>=100){
			break;
		}
		if(u.bx<=0 || u.by<=0 || u.bx>xx || u.by>yy || masu[u.bx][u.by]==1){
			continue;
		}
		if(u.bbx<=0 || u.bby<=0 || u.bbx>xx || u.bby>yy || masu[u.bbx][u.bby]==1){
			continue;
		}
		
		if(memo[u.bx][u.by][u.bbx][u.bby]==1){
			continue;
		}else{
			memo[u.bx][u.by][u.bbx][u.bby]=1;
		}
		if(u.bx==u.bbx && u.by==u.bby){
			return u.dep;
		}
		
		
		rep(i,4){
			hako.push(Q(u.bx+dx[i],u.by+dy[i],u.bbx+dx[(i+2)%4],u.bby+dy[(i+2)%4],u.dep+1));
		}
	}
	return -1;
}

int main(){
	while(1){
		sintt(xx,yy);
		
		if(xx==0){
			break;
		}
		int bx,by,bbx,bby;
		sintt(bx,by);
		sintt(bbx,bby);
		
		rep(i,51)rep(j,51)rep(k,51)rep(p,51){
		memo[i][j][k][p]=0;
		}
		
		rep(i,yy){
			rep(j,xx){
				sint(masu[j+1][i+1]);
			}
		}
		
		int ans=search(bx,by,bbx,bby);
		if(ans==-1){
			puts("NA");
		}else{
			printf("%d\n",ans);
		}
	}
}

C++: #include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<complex>
#include<cmath>
#include<algorithm>
#include<map>
#include<vector>
#include<sstream>
#include<queue>
#include<iostream>
using namespace std;
struct data{
    int x;
    int y;
    int u;
    int v;
    int t;
};

bool vis[60][60][60][60] = {};
const int dx[] = {1,0,-1,0},dy[] = {0,1,0,-1};
int main(void){
    int w,h;
    while(cin>>w>>h,w){
        int x,y,u,v;
        cin>>x>>y>>u>>v;
        bool screen[60][60] = {};
        for(int i = 0;i < h;i++){
            for(int j = 0;j < w;j++){
                cin>>screen[i][j];
            }
        }
        data D;
        D.x = x,D.y = y;
        D.u = u,D.v = v;
        D.t = 0;
        queue<data> Q;
        Q.push(D);

        while(!Q.empty()){
            data E = Q.front();Q.pop();

            vis[E.x][E.y][E.u][E.v] = true;
            vis[E.u][E.v][E.x][E.y] = true;
            if(E.x == E.u&&E.y == E.v){
                cout<<E.t<<endl;
                goto e;
            }
            for(int i = 0;i < 4;i++){

                    data F = E;
                    F.t++;
                    F.x+=dx[i],F.y+=dy[i],F.u+=dx[(i+2)%4],F.v+=dy[(i+2)%4];
                    if(!(0<=F.x&&F.x<w&&0<=F.y&&F.y<h)){
                        F.x = E.x,F.y = E.y;
                    }
                    else if(screen[F.x][F.y])F.x = E.x,F.y = E.y;
                    if(!(0<=F.u&&F.u<w&&0<=F.v&&F.v<h)){
                        F.u = E.u,F.v = E.v;
                    }
                    else if(screen[F.u][F.v])F.u = E.u,F.v = E.v;
                    if(!vis[F.x][F.y][F.u][F.v]&&!vis[F.u][F.v][F.x][F.y]){
                        Q.push(F);
                    }

            }

        }
        cout<<"NA"<<endl;
        e:;
    }


    return 0;
}