/*
 * File: e.cpp
 * Author: Magica
 * Date: 2014.08.26
 ****************************************/
#include <bits/stdc++.h>
using namespace std;
#define Multicase for (int TestCase = 1, TestCaseSum = xint(); TestCase <= TestCaseSum; ++TestCase)
#define __cin__ { cin.sync_with_stdio(0); cin.tie(0); }
#define inject(x) { cerr << "Function: " << __FUNCTION__ << ", Line: " << __LINE__ << ", " << #x << ": " << (x) << endl; }
#define mp make_pair
#define pb push_back
#define init(range, val) memset(range, val, sizeof(range))
#define HashToken 1048575
typedef pair<int, int> pii;
typedef long long ll;
char buf;
inline char xchar() { while (buf = getchar(), isspace(buf)); return buf; }
inline int xint() { while (buf = getchar(), buf < '0' || buf > '9'); int x = buf - '0'; for (; buf = getchar(), buf >= '0' && buf <= '9'; x = x * 10 + buf - '0'); return x; }
inline ll xll() { while (buf = getchar(), buf < '0' || buf > '9'); ll x = buf - '0'; for (; buf = getchar(), buf >= '0' && buf <= '9'; x = x * 10 + buf - '0'); return x; }
inline string xstring() { while (buf = getchar(), buf == ' ' || buf == '\n'); string x = ""; for (x += buf; buf = getchar(), buf != ' ' && buf != '\n' && buf != '\r'; x += buf); return x; }
inline string xline() { while (buf = getchar(), buf == ' ' || buf == '\n'); string x = ""; for (x += buf; buf = getchar(), buf != '\n' && buf != '\r'; x += buf); return x; }
int Dist[5151515], inQ[5151515], Q[5151515], Hash[5151515], Qh, Qt;
const int H[10][2] = {{0, 0},
                      {9, 2}, {1, 3}, {2, 4},
                      {3, 5}, {4, 6}, {5, 7},
                      {6, 8}, {7, 9}, {8, 1}},
          V[10][2] = {{0, 0},
                      {7, 4}, {8, 5}, {9, 6},
                      {1, 7}, {2, 8}, {3, 9},
                      {4, 1}, {5, 2}, {6, 3}};
inline int GetHash(int X)
{
    static int P;
    for (P = X & HashToken; Hash[P] && Hash[P] != X; P = (P + 13) & HashToken);
    Hash[P] = X;
    return P;
}
int p[] = {0, 100000000, 10000000, 1000000, 100000, 10000, 1000, 100, 10, 1};
int Now[15];
int main()
{
    int h, v, i, st, ed, cur, curh, frh, zero, now, tmp;
    while (scanf("%d%d", &h, &v) != EOF && !(h == 0 && v == 0)) {
        st = ed = 0;
        for (i = 0; i < 9; ++i)
            st = st * 10 + xint();
        for (i = 0; i < 9; ++i)
            ed = ed * 10 + xint();
        if (st == ed) {
            printf("0\n");
            continue;
        }
        init(Dist, 60);
        Qh = 1; Qt = 0;
        Q[++Qt] = st;
        inQ[GetHash(st)] = true;
        Dist[GetHash(st)] = 0;
        for (; Qh <= Qt; ) {
            cur = now = Q[Qh];
            frh = GetHash(Q[Qh]);
            ++Qh;
            /* Expand Operators */
             for (i = 9; i > 0; --i) {
                tmp = cur % 10;
                if (tmp == 0) {
                    zero = i;
                    break;
                }
                cur /= 10;
            }
            /* Horizontal */
            /* Left */
            //swap(Now[zero], Now[H[zero][0]]);
            //cur = 0;
            //for (i = 1; i <= 9; ++i)
            //    cur = cur * 10 + Now[i];
            //inject(cur);
            //printf("--- %d %d\n", now, zero);
            //printf("%d\n", now / p[zero] % 10 * p[zero]);
            cur = now - now / p[zero] % 10 * p[zero] - now / p[H[zero][0]] % 10 * p[H[zero][0]]
                      + now / p[zero] % 10 * p[H[zero][0]] + now / p[H[zero][0]] % 10 * p[zero];
            //inject(cur);
            curh = GetHash(cur);
            if (Dist[curh] > Dist[frh] + h) {
                Dist[curh] = Dist[frh] + h;
                if (!inQ[curh]) {
                    if (Qh > 1 && Dist[curh] < Dist[GetHash(Q[Qh])])
                        Q[--Qh] = cur;
                    else
                        Q[++Qt] = cur;
                    inQ[curh] = true;
                }
            }
            //swap(Now[zero], Now[H[zero][0]]);
            /* Right */
            //swap(Now[zero], Now[H[zero][1]]);
            //cur = 0;
            //for (i = 1; i <= 9; ++i)
            //    cur = cur * 10 + Now[i];
            cur = now - now / p[zero] % 10 * p[zero] - now / p[H[zero][1]] % 10 * p[H[zero][1]]
                      + now / p[zero] % 10 * p[H[zero][1]] + now / p[H[zero][1]] % 10 * p[zero];
            curh = GetHash(cur);
            if (Dist[curh] > Dist[frh] + h) {
                Dist[curh] = Dist[frh] + h;
                if (!inQ[curh]) {
                    if (Qh > 1 && Dist[curh] < Dist[GetHash(Q[Qh])])
                        Q[--Qh] = cur;
                    else
                        Q[++Qt] = cur;
                    inQ[curh] = true;
                }
            }
            //swap(Now[zero], Now[H[zero][1]]);
            /* Vertically */
            /* Up */
            //swap(Now[zero], Now[V[zero][0]]);
            //cur = 0;
            //for (i = 1; i <= 9; ++i)
            //    cur = cur * 10 + Now[i];
            cur = now - now / p[zero] % 10 * p[zero] - now / p[V[zero][0]] % 10 * p[V[zero][0]]
                      + now / p[zero] % 10 * p[V[zero][0]] + now / p[V[zero][0]] % 10 * p[zero];
            curh = GetHash(cur);
            if (Dist[curh] > Dist[frh] + v) {
                Dist[curh] = Dist[frh] + v;
                if (!inQ[curh]) {
                    if (Qh > 1 && Dist[curh] < Dist[GetHash(Q[Qh])])
                        Q[--Qh] = cur;
                    else
                        Q[++Qt] = cur;
                    inQ[curh] = true;
                }
            }
            //swap(Now[zero], Now[V[zero][0]]);
            /* Down */
            //swap(Now[zero], Now[V[zero][1]]);
            //cur = 0;
            //for (i = 1; i <= 9; ++i)
            //    cur = cur * 10 + Now[i];
            cur = now - now / p[zero] % 10 * p[zero] - now / p[V[zero][1]] % 10 * p[V[zero][1]]
                      + now / p[zero] % 10 * p[V[zero][1]] + now / p[V[zero][1]] % 10 * p[zero];
            curh = GetHash(cur);
            if (Dist[curh] > Dist[frh] + v) {
                Dist[curh] = Dist[frh] + v;
                if (!inQ[curh]) {
                    if (Qh > 1 && Dist[curh] < Dist[GetHash(Q[Qh])])
                        Q[--Qh] = cur;
                    else
                        Q[++Qt] = cur;
                    inQ[curh] = true;
                }
            }
            //swap(Now[zero], Now[V[zero][1]]);
            inQ[frh] = false;
        }
        printf("%d\n", Dist[GetHash(ed)]);
    }
    return 0;
}

Measured memory_bytes: 38676