#include<iostream>
#include<queue>
#include<map>
#include<cstring>
using namespace std;

struct puz{
	int a[5][5];
	int tim;
};

map<string,bool> memo;
queue<struct puz> que;
struct puz def;
struct puz tmp;
string z;

void erase(){
	for(int i=0; i<5; i++)
		for(int k=0; k<5; k++)
			def.a[i][k] = 20;
	def.tim = 0;
	while(!que.empty())
		que.pop();
	memo.clear();
}

bool clear(){
	for(int i=1; i<4; i++)
		if(def.a[1][i] != i || def.a[3][i] != i+8)
			return false;
	for(int i=0; i<5; i++){
		if(def.a[2][i] != i+4)
			return false;
	}
	return true;
}

void int2str(){
	z.erase();
	for(int i=0; i<5; i++){
		for(int k=0; k<5; k++){
			char t[2];
			t[0] = (char)(def.a[i][k]+48);
			t[1] = '\0';
			z.append(t);
		}
	}
}

void solve(){
	que.push(def);
	while(true){
		if(que.empty()){
			cout << "NA" << endl;
			break;
		}
		def = que.front();
		que.pop();
		
		if(def.tim > 20){
			cout << "NA" << endl;
			break;
		}

		int2str();

		if(memo[z])
			continue;
		else
			memo[z] = true;

		if(clear()){
			cout << def.tim << endl;
			break;
		}

		tmp = def;
		for(int i=0; i<5; i++){
			for(int k=0; k<5; k++){
				if(def.a[i][k] == 0){
					if(i != 0){
						if(tmp.a[i-1][k] != 20){
							tmp.a[i-1][k] = 0;
							tmp.a[i][k] = def.a[i-1][k];
							tmp.tim = def.tim+1;
							que.push(tmp);
							tmp = def;
						}
					}
					if(i != 4){
						if(tmp.a[i+1][k] != 20){
							tmp.a[i+1][k] = 0;
							tmp.a[i][k] = def.a[i+1][k];
							tmp.tim = def.tim+1;
							que.push(tmp);
							tmp = def;
						}
					}
					if(k != 0){
						if(tmp.a[i][k-1] != 20){
							tmp.a[i][k-1] = 0;
							tmp.a[i][k] = def.a[i][k-1];
							tmp.tim = def.tim+1;
							que.push(tmp);
							tmp = def;
						}
					}
					if(k != 4){
						if(tmp.a[i][k+1] != 20){
							tmp.a[i][k+1] = 0;
							tmp.a[i][k] = def.a[i][k+1];
							tmp.tim = def.tim+1;
							que.push(tmp);
							tmp = def;
						}
					}
				}
			}
		}
		
	}
}

int main(){
	while(true){
		erase();
		cin >> def.a[0][2];
		if(!(def.a[0][2]+1))
			return 0;
		cin >> def.a[1][1];
		cin >> def.a[1][2];
		cin >> def.a[1][3];
		cin >> def.a[2][0];
		cin >> def.a[2][1];
		cin >> def.a[2][2];
		cin >> def.a[2][3];
		cin >> def.a[2][4];
		cin >> def.a[3][1];
		cin >> def.a[3][2];
		cin >> def.a[3][3];
		cin >> def.a[4][2];
		solve();
	}
}