#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <complex>
#include <tr1/unordered_set>
#include <tr1/unordered_map>
#include <bitset>
//#pragma comment(linker, "/STACK:1024000000,1024000000")

#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define inf 1e9
#define debug(a) cout << #a" = " << (a) << endl;
#define debugarry(a, n) for (int i = 0; i < (n); i++) { cout << #a"[" << i << "] = " << (a)[i] << endl; }
#define clr(x, y) memset(x, y, sizeof x)
#define LL long long
#define uLL unsigned LL

using namespace std;

const int maxn = 20;
const int maxs = 4e5 + 10;
const int base[9]= {1,1,2,6,24,120,720,5040,40320};

int COST[maxn][maxn];
int goal[maxn];
int cost[maxn][maxn];
vector<pair<int,int> > G[10];

void cca(char a,char b,char c,int v)
{
    //printf("%c | %c %c\n",a,b,c);
    COST[a-'A'][b-'A'] = v;
    COST[a-'A'][c-'A'] = v;
}
void init(int ch,int cv)
{
    for(int i=0; i<9; i++)
        for(int j=0; j<9; j++)
            if(i == j) COST[i][j] = 0;
            else COST[i][j] = inf;
    for (int i = 0; i < 9; i++)
        G[i].clear();
    cca('A','B','I',ch);
    cca('B','C','A',ch);
    cca('C','D','B',ch);
    cca('D','E','C',ch);
    cca('E','F','D',ch);
    cca('F','G','E',ch);
    cca('G','H','F',ch);
    cca('H','I','G',ch);
    cca('I','A','H',ch);

    cca('A','G','D',cv);
    cca('B','H','E',cv);
    cca('C','I','F',cv);
    cca('D','A','G',cv);
    cca('E','B','H',cv);
    cca('F','C','I',cv);
    cca('G','D','A',cv);
    cca('H','E','B',cv);
    cca('I','F','C',cv);

    for (int i = 0; i < 9; i++)
        for (int j = 0; j < 9; j++)
        {
            if (COST[i][j] != inf && i != j)
                G[i].push_back(make_pair(j, COST[i][j]));
        }

    for(int k=0; k<9; k++)
        for(int i=0; i<9; i++)
            for(int j=0; j<9; j++)
                COST[i][j] = min( COST[i][k] + COST[k][j] , COST[i][j] );
}

struct sad
{
    int f[3][3], h, hash_id;
    int x, y;
    bool operator < (const sad & b) const
    {
        return h > b.h;
    }
    int get_hash()
    {
        int a[9],i,j,ii,jj,k=0,ans=0;
        for(i=0; i<3; i++)
        {
            for(j=0; j<3; j++)
                a[k++] = f[i][j];
        }
        for(i=0; i<9; i++)
        {
            k=0;
            for(j=0; j<i; j++)
                if(a[j]>a[i])k++;
            ans += base[i]*k;
        }
        return ans;
    }
    int H()
    {
        return 0;
        int ret = 0;
        for(int i = 0; i < 3; i++)
            for (int j = 0; j < 3; j++)
                ret += COST[f[i][j]][goal[f[i][j]]];
        return ret;
    }
    inline int id()
    {
        return x*3 + y;
    }
    void out()
    {
        printf("-------------------------\n");
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
                printf("%d", f[i][j]);
            printf("\n");
        }
        printf("--------------------------\n");
    }
};
int dis[maxs];
priority_queue<sad> que;
sad org, aim;
int bfs()
{
    for (int i = 0; i < maxs; i++)
        dis[i] = inf;
    while(!que.empty()) que.pop();

    org.hash_id = org.get_hash();
    dis[org.hash_id] = 0;
    org.h = dis[org.hash_id] + org.H();
    que.push(org);

    aim.hash_id = aim.get_hash();
    while(!que.empty())
    {
        sad u = que.top();
        que.pop();
        if (u.hash_id == aim.hash_id)
            break;
        for (int i = 0; i < G[u.id()].size(); i++)
        {
            int x = G[u.id()][i].first/3;
            int y = G[u.id()][i].first%3;
            int cc = G[u.id()][i].second;
            sad v = u;
            swap(v.f[u.x][u.y], v.f[x][y]);
            v.x = x; v.y = y;
            v.hash_id = v.get_hash();
            if (dis[v.hash_id] > dis[u.hash_id] + cc)
            {
                dis[v.hash_id] = dis[u.hash_id] + cc;
                v.h = v.H() + dis[v.hash_id];
                que.push(v);
            }
        }
    }
    return dis[aim.hash_id];
}

int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("A.txt","w",stdout);
    int a,b;
    while(scanf("%d%d",&a,&b) == 2, a||b)
    {
        init(a,b);
//        for(int i=0;i<9;i++)
//            for(int j=0;j<9;j++)
//                printf("(%d,%d)%d\n ",i,j,COST[i][j]);
        for (int i = 0; i < 3; i++)
            for (int j = 0; j < 3; j++)
            {
                scanf("%d", &org.f[i][j]);
                if (org.f[i][j] == 0)
                    org.x = i, org.y = j;
            }
        for (int i = 0; i < 3; i++)
            for (int j = 0; j < 3; j++)
            {
                scanf("%d", &aim.f[i][j]);
                if (aim.f[i][j] == 0)
                    aim.x = i, aim.y = j;
                goal[aim.f[i][j]] = i*3 + j;
            }
        printf("%d\n",bfs());
    }
    return 0;
}

Measured memory_bytes: 17392