Go: package main

import (
	"container/heap"
	"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)
	dx := []int64{0, 1, 0, -1}
	dy := []int64{1, 0, -1, 0}
	memo := make(map[PersonPosition]struct{}, 0)
	for pq.Len() > 0 {
		now := heap.Pop(&pq).(*Status)
		if now.Person.Distance() == 0 {
			fmt.Println(now.Turn)
			return
		}
		if _, ok := memo[now.Person]; ok {
			continue
		}
		memo[now.Person] = struct{}{}
		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)
			}
		}
	}
	fmt.Println("NA")
}

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>
struct D{int t,u,k,l,c;};main(){for(int X,Y,t,u,k,l,d[50][50],a[4]={1,0,-1,0};std::cin>>X>>Y>>t>>u>>k>>l,X;~k?printf("%d\n",k):puts("NA")){std::queue<D> q;D p={t-1,u-1,k-1,l-1,0},r;q.push(p);for(u=0;u<Y;u++)for(t=0;t<X;t++)std::cin>>d[t][u];int v[50][50][50][50]={0};for(k=-1;q.size();){p=q.front();q.pop();if(p.t==p.k&&p.u==p.l){k=p.c;break;}v[p.t][p.u][p.k][p.l]=1;for(t=4;t-->0;){r=p;r.t+=a[t];r.u+=a[(t+3)%4];r.k+=a[(t+2)%4];r.l+=a[(t+1)%4];r.c++;if(r.t<0|X<=r.t|r.u<0|Y<=r.u|d[r.t][r.u]){r.t=p.t;r.u=p.u;}if(r.k<0|X<=r.k|r.l<0|Y<=r.l|d[r.k][r.l]){r.k=p.k;r.l=p.l;}if(!v[r.t][r.u][r.k][r.l])q.push(r);v[r.t][r.u][r.k][r.l]=1;}}}}

C++: #include <iostream>
#include <string>
#include <queue>
using namespace std;

struct Point{
  int tx,ty,kx,ky,time;
};

int main(){
  int i,j,k,l,X,Y,G[51][51],ans;
  bool D[51][51][51][51];
  while(1){
    Point p;
    queue <Point> Q;
    cin >> X >> Y;
    if(X == 0 && Y == 0) break;
    cin >> p.tx >> p.ty;
    cin >> p.kx >> p.ky;

    for(i=1;i<=Y;i++){
      for(j=1;j<=X;j++){
	cin >> G[j][i];
	for(k=1;k<=X;k++){
	  for(l=1;l<=Y;l++) D[j][k][i][l] = false;
	}
      }
    }

    p.time = 0;
    Q.push(p);
    D[p.tx][p.kx][p.ty][p.ky] = true;
    ans = 0;

    while(!Q.empty()){
      p = Q.front();
      Q.pop();
      if(p.tx == p.kx && p.ty == p.ky || p.time >= 100){
	ans = p.time;
	break;
      }

      int x[4] = {1,0,-1,0};
      int y[4] = {0,1,0,-1};
      Point v;

      for(i=0;i<4;i++){
	int ntx = p.tx + x[i];
	int nty = p.ty + y[i];
	int nkx = p.kx + x[(i+2)%4];
	int nky = p.ky + y[(i+2)%4];
	if(ntx > 0 && nkx > 0 && ntx <= X && nkx <= X && nty > 0 && nky > 0 && nty <= Y && nky <= Y && !G[ntx][nty] && !G[nkx][nky] && !D[ntx][nkx][nty][nky]){
	  D[ntx][nkx][nty][nky] = true;
	  v.tx = ntx;
	  v.ty = nty;
	  v.kx = nkx;
	  v.ky = nky;
	  v.time = p.time + 1;
	  Q.push(v);
	}
	else if(nkx > 0 && nkx <= X && nky > 0 && nky <= Y && !G[nkx][nky] && (ntx <= 0 || ntx > X || nty <= 0 || nty > Y || G[ntx][nty]) && !D[p.tx][nkx][p.ty][nky]){
	  D[p.tx][nkx][p.ty][nky] = true;
	  v.tx = p.tx;
	  v.ty = p.ty;
	  v.kx = nkx;
	  v.ky = nky;
	  v.time = p.time + 1;
	  Q.push(v);
	}
	else if(ntx > 0 && ntx <= X && nty > 0 && nty <= Y && !G[ntx][nty] && (nkx <= 0 || nkx > X || nky <= 0 || nky > Y || G[nkx][nky]) &&  !D[ntx][p.kx][nty][p.ky]){
	  D[ntx][p.kx][nty][p.ky] = true;
	  v.tx = ntx;
	  v.ty = nty;
	  v.kx = p.kx;
	  v.ky = p.ky;
	  v.time = p.time + 1;
	  Q.push(v);
	}
      }
    }

    if(ans > 0 && ans < 100) cout << ans << endl;
    else cout << "NA" << endl;
    
  }
  return 0;
}

C++: #include <iostream>
#include <map>
#include <queue>
#include <sstream>

using namespace std;

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

class state {
public:
    int tx, ty;
    int kx, ky;
    int time;
    state(int a, int b, int c, int d, int e) {
        tx = a;
        ty = b;
        kx = c;
        ky = d;
        time = e;
    }
};

int h, w;
int grid[51][51];
int tx, ty, kx, ky;

void solve()
{
    int ans = -1;
    state s(tx, ty, kx, ky, 0);
    map<string, bool> m;
    queue<state> q;
    ostringstream oss;

    oss << tx << "," << ty << "," << kx << "," << ky;
    q.push(s);
    m[oss.str()] = true;

    while (!q.empty()) {
        state now = q.front();
        q.pop();

        if (now.time >= 10)
            break;
        if (now.tx == now.kx && now.ty == now.ky) {
            ans = now.time;
            break;
        }

        for (int i = 0; i < 4; i++) {
            int ntx = now.tx + dx[i];
            int nty = now.ty + dy[i];
            int nkx = now.kx + dx[(i + 2) % 4];
            int nky = now.ky + dy[(i + 2) % 4];

            if (ntx < 1 || ntx > w)
                ntx = now.tx;
            if (nty < 1 || nty > h)
                nty = now.ty;
            if (nkx < 1 || nkx > w)
                nkx = now.kx;
            if (nky < 1 || nky > h)
                nky = now.ky;
            if (grid[nty][ntx] == 1) {
                ntx = now.tx;
                nty = now.ty;
            }
            if (grid[nky][nkx] == 1) {
                nkx = now.kx;
                nky = now.ky;
            }
                
            oss.str("");
            oss << ntx << "," << nty << "," << nkx << "," << nky;

            if (m.count(oss.str()) != 0)
                continue;
            state next(ntx, nty, nkx, nky, now.time + 1);
            q.push(next);
            m[oss.str()] = true;
        }
    }
    if (ans == -1)
        cout << "NA" << endl;
    else
        cout << ans << endl;
}

int main()
{
    while (cin >> w >> h, w || h) {
        cin >> tx >> ty >> kx >> ky;
        for (int y = 1; y <= h; y++)
            for (int x = 1; x <= w; x++) 
                cin >> grid[y][x];
        solve();
    }

    return 0;
}