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 <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>

using namespace std;

#define rep(i,j) REP((i), 0, (j))
#define REP(i,j,k) for(int i=(j);(i)<(k);++i)
#define BW(x,b) ((x)>=0&&(x)<(b))
#define Y first
#define X second
#define INF 1 << 30

typedef pair<int, int> pii;
typedef queue<int> qi;
typedef pair<int, int> pi;

int board[51][51], Y, X;
int dx[] = {0,1,0,-1}, dy[] = {1,0,-1,0};

pii nextp1(pii now, int i){
  pii next = now;
  next.Y += dy[i];
  next.X += dx[i];
  if(BW((next.Y), Y) && BW((next.X), X) && board[next.Y][next.X] == 0) return next;
  return now;
}

pii nextp2(pii now, int i){
  pii next = now;
  next.Y += -1 * dy[i];
  next.X += -1 * dx[i];
  if(BW(next.Y, Y) && BW(next.X, X) && board[next.Y][next.X] == 0) return next;
  return now;
}

pii nextp(pii now, int i, int d){
  pii next = now;
  next.Y += dy[i] * d;
  next.X += dx[i] * d;
  if(BW(next.Y, Y) && BW(next.X, X) && board[next.Y][next.X] == 0) return next;
  return now;
}
     
int main(){
  while(scanf("%d%d", &X,  &Y) && X+Y){
    int tx, ty, kx, ky;
    scanf("%d%d%d%d", &tx, &ty, &kx, &ky); tx--; ty--; kx--; ky--;
    rep(i, Y) rep(j, X) scanf("%d", &board[i][j]); 

    map<pair<pi,pi>, bool>m; // t k
    queue<pii>tq, kq;
    tq.push(make_pair(ty, tx));
    kq.push(make_pair(ky, kx));
    m[make_pair(make_pair(ty, tx), make_pair(ky, kx))] = 1;
    int res = 1;
    while(!tq.empty()){
      if(res >= 100) break;
      int qsize = (int)tq.size();
      rep(i, qsize){
	pii t = tq.front(); tq.pop();
	pii k = kq.front(); kq.pop();
	rep(j, 4){
	  pii nt = nextp(t, j, 1), nk = nextp(k, j, -1);
	  pair<pi, pi> next = make_pair(make_pair(nt.Y, nt.X), make_pair(nk.Y, nk.X));
	  if(m.count(next) == 1) continue;
	  if(nt == nk){
	    printf("%d\n", res);
	    goto end;
	  }
	  m[next] = 1;
	  tq.push(nt); kq.push(nk);
	}
      }
      res++;

    }
    puts("NA");
  end:;
  }
  return 0;
}

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.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<iostream>
#include<algorithm>
using namespace std;
int h,w;
int map[100][100];
int qtx[1000000],qty[1000000],qkx[1000000],qky[1000000];
int ql[1000000];
int tail,head;
int ans;
int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
bool bend;
void BFS(int tx,int ty,int kx,int ky,int len){
	if(len==100)return;
	if(tx==kx&&ty==ky){
		ans=len;
		bend=true;
		return;
	}
	for(int i=0;i<4;i++){
		bool mt=false,mk=false;
		if(tx+dx[i]>=0&&tx+dx[i]<w&&ty+dy[i]>=0&&ty+dy[i]<h){
			if(map[ty+dy[i]][tx+dx[i]]==0){
				mt=true;
			}
		}
		if(kx-dx[i]>=0&&kx-dx[i]<w&&ky-dy[i]>=0&&ky-dy[i]<h){
			if(map[ky-dy[i]][kx-dx[i]]==0){
				mk=true;
			}
		}
		qtx[tail]=tx+mt*dx[i],qty[tail]=ty+mt*dy[i];
		qkx[tail]=kx-mk*dx[i],qky[tail]=ky-mk*dy[i];
		ql[tail]=len+1;
		tail++;
	}
}
int main(){
	while(true){
	cin>>w>>h;
	if(w==0&&h==0)break;
	int tx,ty,kx,ky;
	cin>>tx>>ty>>kx>>ky;
	tx--,ty--,kx--,ky--;
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			cin>>map[i][j];
		}
	}
	ans=869121;
	qtx[0]=tx,qty[0]=ty,qkx[0]=kx,qky[0]=ky;
	ql[0]=0;
	tail=1,head=0;
	bend=false;
	while(true){
		if(tail==head)break;
		BFS(qtx[head],qty[head],qkx[head],qky[head],ql[head]);
		if(bend)break;
		head++;
	}
	if(ans==869121)cout<<"NA"<<endl;
	else cout<<ans<<endl;
	}
	return 0;
}