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)
	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 <cstring>
#include <deque>

using namespace std;

bool f[52][52];
bool visited[52][52][52][52];

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


class state{
public:
    int ax;
    int ay;
    int bx;
    int by;
    int depth;

    state(int a, int b, int c, int d, int e){
        ax = a;
        ay = b;
        bx = c;
        by = d;
        depth = e;
    }
};

int main(){
    int w,h;
    while(cin >> w >> h){
        if((w|h) == 0) break;
        int ax,ay,bx,by,tmp;
        deque<state> q;
        cin >> ax >> ay;
        cin >> bx >> by;
        
        memset(f,false,sizeof(f));
        memset(visited, false, sizeof(visited));

        for(int i=1;i<=h;i++){
            for(int j=1;j<=w;j++){
                cin >> tmp;
                f[i][j] = tmp?0:1;
            }
        }

        state s = state(ax,ay,bx,by,0);
        int tmpd;
        bool isok = false;
        q.push_back(s);
        visited[ay][ax][by][bx] = true;
        while(!q.empty()){
            s = q.front();
            ax = s.ax;
            ay = s.ay;
            bx = s.bx;
            by = s.by;
            tmpd = s.depth;
            q.pop_front();
            if((ax == bx) && (ay == by)){
                cout << tmpd << endl;
                isok = true;
                break;
            }
            if(tmpd == 100){
                break;
            }
            int nax, nay, nbx, nby;
            for(int i=0;i<4;i++){
                nax = ax+dx[i];
                nay = ay+dy[i];
                nbx = bx+dx[(i+2)%4];
                nby = by+dy[(i+2)%4];
                if(f[nay][nax] == false){
                    nay = ay;
                    nax = ax;
                }
                if(f[nby][nbx] == false){
                    nby = by;
                    nbx = bx;
                }
                if(f[nay][nax] == true && f[nby][nbx] == true &&
                   visited[nay][nax][nby][nbx] == false){
                    q.push_back(state(nax,nay,nbx,nby,tmpd+1));
                    visited[nay][nax][nby][nbx] = true;
                }
            }
        }
        if(!isok) cout << "NA" << endl;
    }
}

C++: #include<iostream>
#include<queue>
using namespace std;
struct xy{
  int tx,ty;
  int kx,ky;
  int ti;
};
int x,y;
xy tk;
int m[51][51],vt[51][51]={},vk[51][51]={};
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};

bool g(int a,int b){
  if(a<=0 || a>x || b<=0 || b>x) return 0;
  return 1;
}

int bfs(){
  queue<xy> q;
  xy t,tmp;
  int ta,tb,ka,kb,ans=10000,tim,cnt=0;
  q.push(tk);
  while(!q.empty()){
    t=q.front(),q.pop();
      for(int i=0;i<4;i++){
      ta=t.tx+dx[i],tb=t.ty+dy[i],ka=t.kx-dx[i],kb=t.ky-dy[i],tim=t.ti+1;
      if(g(ta,tb) || g(ka,kb)){
	if(vt[ta][tb]==2 && vt[ta][tb]==2)continue;
	if(m[ta][tb]==1) tmp.tx=t.tx,tmp.ty=t.ty;
	else if(vt[ta][tb]==0 && m[ta][tb]!=1)tmp.tx=ta,tmp.ty=tb,vt[ta][tb]=2;
	if(m[ta][tb]==1) tmp.kx=t.kx,tmp.ky=t.ky;
	else if(vk[ka][kb]==0 && m[ka][kb]!=1)tmp.kx=ka,tmp.ky=kb,vt[ta][tb]=2;
	tmp.ti=tim;
	cnt++;
	if(tmp.ti>=100 ||cnt>=100)  break;
	if(tmp.tx==tmp.kx && tmp.ky==tmp.ty){
	  if(ans>tmp.ti) ans=tmp.ti;
	  continue;
	}
	q.push(tmp);
      }
    }
  }
  return ans;
}

int main(){
  while(1){
    int ans;
    cin>>x>>y;
    if(x+y==0) break;
    cin>>tk.tx>>tk.ty>>tk.kx>>tk.ky;
    for(int i=1;i<=x;i++)
      for(int j=1;j<=y;j++) vt[i][j]=0,vk[i][j]=0;
    tk.ti=0;
    for(int i=1;i<=x;i++)
      for(int j=1;j<=y;j++)  cin>>m[i][j];
    ans=bfs();
    if(ans<100) cout<<ans<<endl;
    else cout<<"NA"<<endl;
  }
  
  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

int dp[52][52][52][52];
void solve(int h, int w){
    for(auto &i : dp) for(auto &j : i) for(auto &k : j) for(auto &l : k) l = INF<int>;
    int tx, ty, kx, ky;
    cin >> tx >> ty >> kx >> ky;
    vector<vector<int>> G(h+2, vector<int>(w+2, 1));
    for (int i = 0; i < h; ++i) {
        for (int j = 0; j < w; ++j) {
            cin >> G[i+1][j+1];
        }
    }
    array<int, 4> dx{-1, 1, 0, 0}, dy{0, 0, -1, 1};
    queue<tuple<int, int, int, int>> Q;
    Q.emplace(ty, tx, ky, kx);
    dp[ty][tx][ky][kx] = 0;
    while(!Q.empty()){
        int y1, x1, y2, x2; tie(y1, x1, y2, x2) = Q.front(); Q.pop();
        if(dp[y1][x1][y2][x2] >= 100) break;
        if(y1 == y2 && x1 == x2){
            cout << dp[y1][x1][y2][x2] << "\n";
            return;
        }
        for (int k = 0; k < 4; ++k) {
            int Y1 = y1+dy[k], X1 = x1+dx[k], Y2 = y2-dy[k], X2 = x2-dx[k];
            if(G[Y1][X1]) Y1 -= dy[k], X1 -= dx[k];
            if(G[Y2][X2]) Y2 += dy[k], X2 += dx[k];
            if(dp[Y1][X1][Y2][X2] > dp[y1][x1][y2][x2]+1){
                Q.emplace(Y1, X1, Y2, X2);
                dp[Y1][X1][Y2][X2] = dp[y1][x1][y2][x2]+1;
            }
        }
    }
    puts("NA");
}

int main() {
    int w, h;
    while(cin >> w >> h, w){
        solve(h, w);
    }
    return 0;
}