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<iostream>
#include<queue>
using namespace std;

const int INF = 100000000;

class Point{
public:
  int x;
  int y;
  int x2;
  int y2;
};

int main(){
  int N,M;
  cin >> N >> M;
  char F[102][102];
  for(int y=0; y<=N+1; y++){
    for(int x=0; x<=M+1; x++){
      F[y][x] = 1;
    }
  }

  int tx,ty;
  int kx,ky;
  cin >> tx >> ty;
  cin >> kx >> ky;

  for(int y=1; y<=N; y++){
    for(int x=1; x<=M; x++){
      cin >> F[y][x];
    }
  }

  int D[102][102];
  for(int y=1; y<=N; y++){
    for(int x=1; x<=M; x++){
      D[y][x] = INF;
    }
  }

  queue<Point> que;

  Point p;
  p.x = tx; p.y = ty;
  p.x2 = kx; p.y2 = ky;
  que.push(p);
  D[p.y][p.x] = 0;

  while(!que.empty()){
    p = que.front(); que.pop();

    if(p.x==p.x2 && p.y==p.y2){
      cout << D[p.y][p.x] << endl;
      cout << p.x << p.y << endl;
      break;
    }

    if(F[p.y-1][p.x]!= 1 && F[p.y2+1][p.x2]!= 1 && D[p.y-1][p.x]==INF){
      Point q;
      q.x = p.x; q.y = p.y-1;
      q.x2 = p.x2; q.y2 = p.y2+1;

      if(F[q.x][q.y]==1){
        q.y = p.y;
      }else if(F[q.x2][q.y2]==1){
        q.y2 = p.y2;
      }

      que.push(q);
      D[q.y][q.x] = D[p.y][p.x]+1;
    }
    if(F[p.y+1][p.x]!=1 && F[p.y2-1][p.x2]!=1 && D[p.y+1][p.x]==INF){
      Point q;
      q.x = p.x; q.y = p.y+1;
      q.x2 = p.x2; q.y2 = p.y2-1;

      if(F[q.x][q.y]==1){
        q.y = p.y;
      }else if(F[q.x2][q.y2]==1){
        q.y2 = p.y2;
      }

      que.push(q);
      D[q.y][q.x] = D[p.y][p.x]+1;
    }
    if(F[p.y][p.x-1]!=1 && F[p.y2][p.x2+1]!=1 && D[p.y][p.x-1]==INF){
      Point q;
      q.x = p.x-1; q.y = p.y;
      q.x2 = p.x2+1; q.y2 = p.y2;

      if(F[q.x][q.y]==1){
        q.x = p.x;
      }else if(F[q.x2][q.y2]==1){
        q.x2 = p.x2;
      }

      que.push(q);
      D[q.y][q.x] = D[p.y][p.x]+1;
    }
    if(F[p.y][p.x+1]!=1 && F[p.y2][p.x2-1]!=1 && D[p.y][p.x+1]==INF){
      Point q;
      q.x = p.x+1; q.y = p.y;
      q.x2 = p.x2-1; q.y2 = p.y2;

      if(F[q.x][q.y]==1){
        q.x = p.x;
      }else if(F[q.x2][q.y2]==1){
        q.x2 = p.x2;
      }

      que.push(q);
      D[q.y][q.x] = D[p.y][p.x]+1;
    }

  }

  return 0;
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>	// require sort next_permutation count __gcd reverse etc.
#include <cstdlib>	// require abs exit atof atoi 
#include <cstdio>		// require scanf printf
#include <functional>
#include <numeric>	// require accumulate
#include <cmath>		// require fabs
#include <climits>
#include <limits>
#include <cfloat>
#include <iomanip>	// require setw
#include <sstream>	// require stringstream 
#include <cstring>	// require memset
#include <cctype>		// require tolower, toupper
#include <fstream>	// require freopen
#include <ctime>		// require srand
#define rep(i,n) for(int i=0;i<(n);i++)
#define ALL(A) A.begin(), A.end()
#define INF 1<<30

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<P,P> PP;
typedef pair<PP,int> PPi;

int grid[50][50];
bool vis[50][50][50][50];

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

struct POS{
	int tx, ty, kx, ky, t;
	POS (int _tx, int _ty, int _kx, int _ky, int _t ) : tx(_tx), ty(_ty), kx(_kx), ky(_ky), t(_t) {}
//	bool operator>(const POS &a ) const{ return t > a.t; }
};

int main()
{
//	cut here before submit 
//	freopen ("testcase.stray_twins", "r", stdin );
	int w, h;
	while (scanf ("%d %d", &w, &h ) && w && h ){
		memset (grid, 0, sizeof (grid ) );
		memset (vis, false, sizeof (vis ) );
			
		int tx, ty;
		scanf ("%d %d", &tx, &ty );
		tx--, ty--;
		int kx, ky;
		scanf ("%d %d", &kx, &ky );
		kx--, ky--;
		rep (i, h ){
			rep (j, w ){
				scanf ("%d", &grid[i][j] );
			} // end rep
		} // end rep

		vis[tx][ty][kx][ky] = true;
//		priority_queue<POS, vector<POS>, greater<POS> > que;
		queue<POS> que;
		POS first (tx, ty, kx, ky, 0 );
		que.push (first );
		int res = INF;
		while (!que.empty() ){
			POS curr = que.front(); que.pop();
			int ctx = curr.tx;
			int cty = curr.ty;
			int ckx = curr.kx;
			int cky = curr.ky; 
			int ct  = curr.t;
			rep (k, 4 ){
				int ntx = ctx + dx[k], nty = cty + dy[k];
				int nkx = ckx - dx[k], nky = cky - dy[k];
				if (ntx < 0 || ntx >= w || nty < 0 || nty >= h || grid[nty][ntx]){
					ntx = tx, nty = ty;
				} // end if
				if (nkx < 0 || nkx >= w || nky < 0 || nky >= h || grid[nky][nkx]){
					nkx = kx, nky = ky;
				} // end if
				if (vis[ntx][nty][nkx][nky] || ct + 1 >= 100 ) continue;
				if (ntx == nkx && nty == nky ){
					res = ct+1;
					goto ans;
				} // end if
				vis[ntx][nty][nkx][nky] = true;
				POS next (ntx, nty, nkx, nky, ct+1 ); 
				que.push (next );
			} // end rep
		} // end while
ans:;
		if (res != INF ){
			printf ("%d\n", res );
		}else{
			printf ("NA\n" );
		} // end if

	} // end loop
		
	return 0;
}