Language: C++

#include <queue>
#include <cstdio>
int n, a[100], b[100], d[100][2];
int main() {
	while(scanf("%d", &n), n) {
		for(int i = 0; i < n; i++) scanf("%d", &a[i]);
		for(int i = 0; i < n; i++) scanf("%d", &b[i]);
		for(int i = 1; i < n; i++) d[i][0] = -1, d[i][1] = -1;
		d[0][0] = 0, d[0][1] = 0;
		queue<int> que; que.push(0); que.push(1);
		while(!que.empty()) {
			int x = que.top() / 2, y = que.top() % 2; que.pop();
			for(int i = 0; i <= 2 && x + i < n; i++) {
				if(y == 1) {
					int tx = x + i;
					if(a[tx] == 1) {
						for(; tx + 1 < n; tx++) {
							if(a[tx + 1] != 1) break;
						}
					}
					else if(a[tx] == 2) {
						for(; tx - 1 > 0; tx--) {
							if(a[tx - 1] != 2) break;
						}
					}
					if(d[tx][0] > d[x][y] + 1) {
						d[tx][0] = d[x][y] + 1;
						que.push_back(make_pair(tx * 2));
					}
				}
				else {
					int tx = x + i;
					if(b[tx] == 1) {
						for(; tx + 1 < n; tx++) {
							if(b[tx + 1] != 1) break;
						}
					}
					else if(b[tx] == 2) {
						for(; tx - 1 > 0; tx--) {
							if(b[tx - 1] != 2) break;
						}
					}
					if(d[tx][1] == -1) {
						d[tx][1] = d[x][y] + 1;
						que.push_back(make_pair(tx * 2 + 1));
					}
				}
			}
		}
		int r = min(d[n - 1][0], d[n - 1][1]);
		if(r == -1) printf("NA\n");
		else printf("%d\n", r);
	}
	return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:10:17: error: 'queue' was not declared in this scope; did you mean 'std::queue'?
   10 |                 queue<int> que; que.push(0); que.push(1);
      |                 ^~~~~
      |                 std::queue
In file included from /usr/include/c++/14/queue:66,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_queue.h:96:11: note: 'std::queue' declared here
   96 |     class queue
      |           ^~~~~
a.cc:10:23: error: expected primary-expression before 'int'
   10 |                 queue<int> que; que.push(0); que.push(1);
      |                       ^~~
a.cc:10:33: error: 'que' was not declared in this scope
   10 |                 queue<int> que; que.push(0); que.push(1);
      |                                 ^~~
a.cc:14:36: error: 'y' was not declared in this scope
   14 |                                 if(y == 1) {
      |                                    ^
a.cc:28:63: error: 'make_pair' was not declared in this scope; did you mean 'std::make_pair'?
   28 |                                                 que.push_back(make_pair(tx * 2));
      |                                                               ^~~~~~~~~
      |                                                               std::make_pair
In file included from /usr/include/c++/14/bits/stl_algobase.h:64,
                 from /usr/include/c++/14/deque:62,
                 from /usr/include/c++/14/queue:62:
/usr/include/c++/14/bits/stl_pair.h:1132:5: note: 'std::make_pair' declared here
 1132 |     make_pair(_T1&& __x, _T2&& __y)
      |     ^~~~~~~~~
a.cc:45:63: error: 'make_pair' was not declared in this scope; did you mean 'std::make_pair'?
   45 |                                                 que.push_back(make_pair(tx * 2 + 1));
      |                                                               ^~~~~~~~~
      |                                                               std::make_pair
/usr/include/c++/14/bits/stl_pair.h:1132:5: note: 'std::make_pair' declared here
 1132 |     make_pair(_T1&& __x, _T2&& __y)
      |     ^~~~~~~~~
a.cc:50:25: error: 'min' was not declared in this scope; did you mean 'std::min'?
   50 |                 int r = min(d[n - 1][0], d[n - 1][1]);
      |                         ^~~
      |                         std::min
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: 'std::min' declared here
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~