// by team3
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <climits>
#include <cassert>
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <complex>

using namespace std;

#define rep(i,n) for (int i = 0; i < (n); ++i)
#define For(i,s,t) for (int i = (s); i <= (t); ++i)
#define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)

typedef long long LL;
typedef pair<int, int> Pii;

const int inf = 0x3f3f3f3f;
const LL infLL = 0x3f3f3f3f3f3f3f3fLL;

int fac[9];

struct node
{
    int a[9];
    int zero;
    int cantorid;
    int cantor()
    {
        int ans = 0;
        for (int i = 0; i < 9; ++i)
        {
            int tot = 0;
            for (int j = i + 1; j < 9; ++j)
                if (a[i] > a[j])
                    ++tot;
            ans += tot * fac[8 - i];
        }
        return ans;
    }

    int id(int x, int y)
    {
        return x * 3 + y;
    }

    void reset()
    {
        for (int i = 0; i < 9; ++i)
            if (a[i] == 0)
            {
                zero = i;
                break;
            }
        cantorid = cantor();
    }

    void read()
    {
        for (int i = 0; i < 9; ++i)
        {
            scanf("%d", &a[i]);
            if (a[i] == 0) zero = i;
        }
    }
};
const int maxn = 370000;
const int ax[] = {0, 1, 0, -1};
const int ay[] = {1, 0, -1, 0};

int cx, cy, beid, enid;
node be, en;
int e[maxn][4];
bool inq[maxn];

void init()
{
    be.read();
    en.read();
}

node decantor(int x)
{
    node a;
    int b[9];
    for (int i = 0; i < 9; ++i) b[i] = i;
    for (int i = 0; i < 9; ++i)
    {
        int cnt = x / fac[8 - i];
        a.a[i] = b[cnt];
        for (int j = cnt; j < 8 - i; ++j)
            b[j] = b[j + 1];
        x %= fac[8 - i];
    }
    a.reset();
    return a;
}

void build()
{
    int lable = 1;
    for (int i = 1; i <= 9; ++i)
        lable *= i;
    for (int i = 0; i < lable; ++i)
    {
        node u = decantor(i);
        int x = u.zero / 3, y = u.zero % 3;
        for (int k = 0; k < 4; ++k)
        {
            int xx = x + ax[k], yy = y + ay[k];
            if (xx < 0) xx = 2;
            else if (xx > 2) xx = 0;
            else if (yy < 0)
            {
                yy = 2;
                if (xx == 0) xx = 2;
                else if (xx == 1) xx = 0;
                else xx = 1;
            }
            else if (yy > 2)
            {
                yy = 0;
                if (xx == 0) xx = 1;
                else if (xx == 1) xx = 2;
                else xx = 0;
            }
            node v(u);
            swap(v.a[u.zero], v.a[xx * 3 + yy]);
            e[i][k] = v.cantor();
        }
    }
}

int d[maxn];
queue<int> que;
int add[4];

void solve()
{
    add[0] = add[2] = cy;
    add[1] = add[3] = cx;
    int beid = be.cantor();
    int enid = en.cantor();
    memset(inq, 0, sizeof(inq));
    memset(d, 0x3f, sizeof(d));
    d[beid] = 0; inq[beid] = true;
    while (!que.empty()) que.pop();
    que.push(beid);

    while (!que.empty())
    {
        int u = que.front(); que.pop();
        inq[u] = false;
        for (int k = 0; k < 4; ++k)
        {
            int v = e[u][k];
            if (d[v] > d[u] + add[k])
            {
                d[v] = d[u] + add[k];
                if (!inq[v])
                {
                    inq[v] = true;
                    que.push(v);
                }
            }
        }
    }

    cout << d[enid] << endl;
}

void test()
{
    node a = decantor(362879);
    for (int i = 0; i < 9; ++i)
        cout << a.a[i] << ' ';
    cout << endl;
}

int main()
{
    fac[0] = 1;
    for (int i = 1; i < 9; ++i)
        fac[i] = fac[i - 1] * i;
    build();
    while (scanf("%d%d", &cy, &cx) == 2 && cx + cy)
    {
        init();
        solve();
    }
}

Measured memory_bytes: 9140