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 <queue>
#include <complex>
#include <map>
#include <set>

using namespace std;

typedef complex<int> C;
typedef pair<C, C> P;
typedef pair<int, P> D;

namespace std {
    template<class T>
        bool operator<(const complex<T> &a, const complex<T> &b) {
            return a.real() == b.real()? a.imag() < b.imag(): a.real() < b.real();
        }
}

int main() {
    C dr[] = {C(1, 0), C(0, 1), C(-1, 0), C(0, -1)};
    int x, y, tx, ty, kx, ky;
    int map_[50][50];
    while (true) {
        cin >> x >> y;
        if (x == 0) {
            break;
        }
        cin >> tx >> ty >> kx >> ky;
        for (int i=0; i<y; i++) {
            for (int j=0; j<x; j++) {
                cin >> map_[j][i];
            }
        }
        bool is_found = false;
        set<P> visited;
        visited.insert(P(C(tx-1, ty-1), C(kx-1, ky-1)));
        queue<D> q;
        q.push(D(0, P(C(tx-1, ty-1), C(kx-1, ky-1))));
        while (q.size() > 0 && !is_found) {
            D d = q.front();
            int t = d.first;
            C tr_origin = d.second.first;
            C kr_origin = d.second.second;
            if (t == 99) {
                break;
            }
            for (int i=0; i<4; i++) {
                C tr = tr_origin+dr[i];
                C kr = kr_origin-dr[i];
                if (tr.real() < 0 || tr.imag() < 0 ||
                    tr.real() >= x || tr.imag() >= y ||
                    map_[tr.real()][tr.imag()] == 1) {
                    tr = tr_origin;
                }
                if (kr.real() < 0 || kr.imag() < 0 ||
                    kr.real() >= x || kr.imag() >= y ||
                    map_[kr.real()][kr.imag()] == 1) {
                    kr = kr_origin;
                }
                if (tr == kr) {
                    cout << t+1 << endl;
                    is_found = true;
                    break;
                }
                if (visited.find(P(tr, kr)) == visited.end()) {
                    visited.insert(P(tr, kr));
                    q.push(D(t+1, P(tr, kr)));
                }
            }
            q.pop();
        }
        if (!is_found) {
            cout << "NA" << endl;
        }
    }
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <array>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <tuple>
#include <bitset>
#include <memory>
#include <cmath>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <numeric>
#include <climits>
#include <cfloat>
struct Coordinate {
	int x, y;
	std::vector<Coordinate> neighbors()const { return { Coordinate{x + 1, y}, Coordinate{x - 1, y}, Coordinate{x, y + 1}, Coordinate{x, y - 1} }; }
	Coordinate operator-(const Coordinate& that) const { return Coordinate{ x - that.x, y - that.y }; }
	Coordinate operator+(const Coordinate& that) const { return Coordinate{ x + that.x, y + that.y }; }
	bool operator==(const Coordinate& that) const { return x == that.x && y == that.y; }
	bool operator<(const Coordinate& that) const { return x == that.x ? y < that.y : x < that.x; }
};

int main() {
	std::cin.tie(0); std::cin.sync_with_stdio(false);
	while (true) {
		int w, h; std::cin >> w >> h; if (w == 0 && h == 0) break;
		Coordinate takayuki, kazuyuki; std::cin >> takayuki.x >> takayuki.y >> kazuyuki.x >> kazuyuki.y;
		std::vector<std::vector<int>> state(h + 2, std::vector<int>(w + 2, 1));
		for (auto i = 1; i <= h; ++i) for (auto j = 1; j <= w; ++j) std::cin >> state[i][j];
		if (takayuki == kazuyuki) {
			std::cout << 0 << '\n';
			continue;
		}
		auto comparator = [](const std::pair<Coordinate, Coordinate>& a, const std::pair<Coordinate, Coordinate>& b) {return a.first == b.first ? a.second < b.second : a.first < b.first; };
		std::set<std::pair<Coordinate, Coordinate>> memo;
		std::queue<std::pair<Coordinate, Coordinate>> queue; queue.emplace(takayuki, kazuyuki);
		bool meet = false;
		for (auto i = 1; i <= 100 && !meet; ++i) {
			for (int j = queue.size(); j > 0 && !meet; --j) {
				auto first = queue.front().first;
				auto second = queue.front().second; queue.pop();
				for (auto next_first : first.neighbors()) {
					auto next_second = second + (first - next_first);
					if (state[next_first.y][next_first.x] == 0 || state[next_second.y][next_second.x] == 0) {
						if (state[next_first.y][next_first.x] == 1) next_first = first;
						if (state[next_second.y][next_second.x] == 1) next_second = second;
						if (next_first == next_second) {
							std::cout << i << '\n';
							meet = true;
							break;
						}
						auto pair = memo.emplace(next_first, next_second);
						if (pair.second) {
							queue.emplace(next_first, next_second);
						}
					}
				}
			}
		}
		if (!meet) {
			std::cout << "NA\n";
		}
	}
}