Go: // BFS

package main

import (
	"bufio"
	"os"
		"fmt"
	"strconv"
	"strings"
)

var scanner = bufio.NewScanner(os.Stdin)

var p = [15]int{}
var g = [13][4]int {
	// -1は存在しないマス目
	{2,-1,-1,-1},
	{2,5,-1,-1},{0,1,3,6},{2,7,-1,-1},
	{5,-1,-1,-1},{1,4,6,9},{2,5,7,10},{3,6,8,11},{7,-1,-1,-1},
	{5,10,-1,-1},{6,9,11,12},{7,10,-1,-1},
	{10,-1,-1,-1},
}

/*
　  ０ １ ２ ３ ４
 0 |／|／|  |／|／|
 1 |／|　|  |　|／|
 2 |　|　|  |　|　|
 3 |／|　|  |　|／|
 4 |／|／|  |／|／|
*/

var row = [13]int{0,1,1,1,2,2,2,2,2,3,3,3,4}
var col = [13]int{2,1,2,3,0,1,2,3,4,1,2,3,2}

type wolf struct {
	m [13]int
}

type pair struct {
	first wolf
	second int
}

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

func print_board(board [13]int) {
	fmt.Println("--------------------------")
	fmt.Printf("        [%2d]\n", board[0])
	fmt.Printf("    [%2d][%2d][%2d]\n", board[1], board[2], board[3])
	fmt.Printf("[%2d][%2d][%2d][%2d][%2d]\n", board[4], board[5], board[6], board[7], board[8])
	fmt.Printf("    [%2d][%2d][%2d]\n", board[9], board[10], board[11])
	fmt.Printf("        [%2d]\n", board[12])
	fmt.Println("--------------------------")
}

func main() {
	for scanner.Scan() {
		p1, _ := strconv.Atoi(scanner.Text())
		if p1 == -1 {
			break
		}
		p[0]= p1
		scanner.Scan()
		buf1 := strings.Split(scanner.Text(), " ")
		p2, _ := strconv.Atoi(buf1[0])
		p3, _ := strconv.Atoi(buf1[1])
		p4, _ := strconv.Atoi(buf1[2])
		p[1], p[2], p[3] = p2, p3, p4
		scanner.Scan()
		buf2 := strings.Split(scanner.Text(), " ")
		p5, _ := strconv.Atoi(buf2[0])
		p6, _ := strconv.Atoi(buf2[1])
		p7, _ := strconv.Atoi(buf2[2])
		p8, _ := strconv.Atoi(buf2[3])
		p9, _ := strconv.Atoi(buf2[4])
		p[4], p[5], p[6], p[7], p[8] = p5, p6, p7, p8, p9
		scanner.Scan()
		buf3 := strings.Split(scanner.Text(), " ")
		p10, _ := strconv.Atoi(buf3[0])
		p11, _ := strconv.Atoi(buf3[1])
		p12, _ := strconv.Atoi(buf3[2])
		p[9], p[10], p[11] = p10, p11, p12
		scanner.Scan()
		p13, _ := strconv.Atoi(scanner.Text())
		p[12] = p13
		S := make(map[wolf]struct{})
		st := wolf{}
		for i := 0; i < 13; i++ {
			st.m[i] = p[i]
		}
		Q := make([]pair, 0)
		S[st] = struct{}{}
		Q = append(Q, pair{st, 0})
		yet := true
		for len(Q) > 0 {
			at := Q[0].first
			print_board(at.m)
			cost := Q[0].second
			Q = Q[1:]
			ok := true
			for i :=1; i < 12; i++ {
				if at.m[i] != i {
					ok = false
				}
			}
			if ok {
				// print_board(at.m)
				yet = false
				fmt.Println(cost)
				break
			}
			now := cost
			for i := 0; i < 13; i++ {
				if at.m[i] != 0 {
					now += abs(row[i]-row[at.m[i]]) + abs(col[i]-col[at.m[i]])
				}
			}
			if now > 20 {
				continue
			}
			for i := 0; i < 13; i++ {
				if at.m[i] == 0 {
					for j := 0; j < 4; j++ {
						if ^g[i][j] != 0 {
							at.m[i], at.m[g[i][j]] = at.m[g[i][j]], at.m[i]
							if _, ok := S[at]; !ok {
								S[at] = struct{}{}
								Q = append(Q, pair{at, cost+1})
							}
							at.m[i], at.m[g[i][j]] = at.m[g[i][j]], at.m[i]
						}
					}
				}
			}
		}
		if yet {
			fmt.Println("NA")
		}
	}
}

C++: #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 > 15){
			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();
	}
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>

using namespace std;

#define rep(i,j) REP((i), 0, (j))
#define REP(i,j,k) for(int i=(j);(i)<(k);++i)
#define BW(a,x,b) ((a)<=(x)&&(x)<=(b))
#define F first
#define S second
#define INF 1 << 30

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

vi p, next, now;
vector<vector<int> >dir(13);

int main(){
  rep(i, 13) if(i == 0 || i == 12) p.push_back(0); else p.push_back(i);

  dir[0].push_back(2);
  dir[1].push_back(2); dir[1].push_back(5);
  dir[2].push_back(0); dir[2].push_back(1); dir[2].push_back(3); dir[2].push_back(6);
  dir[3].push_back(2); dir[3].push_back(7);
  dir[4].push_back(5);
  dir[5].push_back(1); dir[5].push_back(4); dir[5].push_back(6); dir[5].push_back(9);
  dir[6].push_back(2); dir[6].push_back(5); dir[6].push_back(7); dir[6].push_back(10);
  dir[7].push_back(3); dir[7].push_back(6); dir[7].push_back(8); dir[7].push_back(11);
  dir[8].push_back(7);
  dir[9].push_back(5); dir[9].push_back(10);
  dir[10].push_back(6); dir[10].push_back(9); dir[10].push_back(11); dir[10].push_back(13);
  dir[11].push_back(7); dir[11].push_back(10);
  dir[12].push_back(10);

  map<vi, int>m;
  queue<vi>q;
  q.push(p);
  m[p] = 0;
  rep(i, 10){
    int qsize = q.size();
    rep(j, qsize){
      now = q.front(); q.pop();
      rep(k, 13){
	if(now[k] != 0) continue;
	rep(d, dir[k].size()){
	  next = now;
	  swap(next[k], next[dir[k][d]]);
	  if(m.count(next) != 0) continue;
	  m[next] = i+1;
	  q.push(next);
	}
      }
    }
  }

  p.resize(13);
  while(scanf("%d", &p[0]) && p[0] != -1){
    REP(i, 1, 13) scanf("%d", &p[i]);

    map<vi, int>dist;
    queue<vi>q;
    q.push(p);
    dist[p] = 0;
    if(m.count(p) != 0){
      printf("%d\n", m[p]);
      goto e;
    }

    rep(i, 10){
      int qsize = q.size();
      rep(j, qsize){
	now = q.front(); q.pop();
	rep(k, 13){
	  if(now[k] != 0) continue;
	  rep(d, dir[k].size()){
	    next = now;
	    swap(next[k], next[dir[k][d]]);
	    if(m.count(next) != 0){
	      printf("%d\n", m[next]+i+1);
	      goto e;
	    }
	    if(dist.count(next) != 0) continue;
	    dist[next] = i+1;
	    q.push(next);
	  }
	}
      }
    }
    puts("NA");
  e:;
  }
  return 0;
}