#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

enum LOC{
	A,
	B,
	C,
	D,
	E,
	F,
	G,
	H,
	I,
};

struct Info{
	int table[9];
	int sum_cost;
	LOC zero_loc;
};

map<int,int> MAP;
int min_cost[362880];
int cost_yoko,cost_tate;
LOC loc_array[9] = {A,B,C,D,E,F,G,H,I};
LOC ADJ[9][4] = {{B,I,D,G},{C,A,H,E},{D,B,I,F},{E,C,A,G},{F,D,B,H},{G,E,C,I},{H,F,D,A},{I,G,E,B},{A,H,F,C}};

int makeNUM(int table[9]){

	int ret = 0;

	for(int i = 0; i < 9; i++){
		ret = 10*ret + table[i];
	}

	return MAP[ret];
}

int getNUM(int num){
	switch(num){
	case 1:return 1;
	case 2:return 2;
	case 3:return 6;
	case 4:return 24;
	case 5:return 120;
	case 6:return 720;
	case 7:return 5040;
	case 8:return 40320;
	}
	return -1;
}

void recursive(bool check[9],int array[9],int num){

	if(num == 9){

		int number = 0;
		for(int i = 0; i < 9; i++){
			number = 10*number + array[i];
		}

		int hanten_num,value = 0;

		for(int i = 0; i < 8; i++){
			hanten_num = 0;
			for(int k = i+1; k < 9; k++){
				if(array[i] > array[k])hanten_num++;
			}
			value += (getNUM(8-i))*hanten_num;
		}
		MAP[number] = value;

		return ;
	}

	for(int i = 0; i < 9; i++){
		if(check[i] == false){
			bool next_check[9];

			for(int k = 0; k < 9; k++)next_check[k] = check[k];
			next_check[i] = true;

			int next_array[9];
			for(int k = 0; k < num; k++)next_array[k] = array[k];
			next_array[num] = i;

			recursive(next_check,next_array,num+1);
		}
	}

}

void makeMAP(){

	bool first[9];

	for(int i = 0; i < 9; i++)first[i] = 0;

	int first_array[9];

	recursive(first,first_array,0);
}

void func(){

	Info first;
	for(int i = 0; i < 9; i++){
		scanf("%d",&first.table[i]);
		if(first.table[i] == 0){
			first.zero_loc = loc_array[i];
		}
	}

	first.sum_cost = 0;

	Info goal;
	for(int i = 0; i < 9; i++){
		scanf("%d",&goal.table[i]);
	}

	for(int i = 0; i < 362880; i++)min_cost[i] = BIG_NUM;

	queue<Info> Q;
	min_cost[makeNUM(first.table)] = 0;
	Q.push(first);

	bool FLG;
	int adj_row,adj_col;

	int minimum = BIG_NUM,tmp;

	while(!Q.empty()){

		if(Q.front().sum_cost > min_cost[makeNUM(Q.front().table)]){
			Q.pop();
			continue;
		}

		if(Q.front().sum_cost >= minimum){
			Q.pop();
			continue;
		}

		FLG = true;
		for(int i = 0; i < 9; i++){
			if(Q.front().table[i] != goal.table[i]){
				FLG = false;
				break;
			}
		}
		if(FLG){
			minimum = min(minimum,Q.front().sum_cost);
			Q.pop();
			continue;
		}

		for(int i = 0; i <= 1; i++){
			Info next_info;
			for(int a = 0; a < 9; a++)next_info.table[a] = Q.front().table[a];
			swap(next_info.table[Q.front().zero_loc],next_info.table[ADJ[Q.front().zero_loc][i]]);
			next_info.zero_loc = ADJ[Q.front().zero_loc][i];
			next_info.sum_cost = Q.front().sum_cost+cost_yoko;
			tmp = makeNUM(next_info.table);
			if(min_cost[tmp] > next_info.sum_cost){
				min_cost[tmp] = next_info.sum_cost;
				Q.push(next_info);
			}
		}

		for(int i = 2; i <= 3; i++){
			Info next_info;
			for(int a = 0; a < 9; a++)next_info.table[a] = Q.front().table[a];
			swap(next_info.table[Q.front().zero_loc],next_info.table[ADJ[Q.front().zero_loc][i]]);
			next_info.zero_loc = ADJ[Q.front().zero_loc][i];
			next_info.sum_cost = Q.front().sum_cost+cost_tate;
			tmp = makeNUM(next_info.table);
			if(min_cost[tmp] > next_info.sum_cost){
				min_cost[tmp] = next_info.sum_cost;
				Q.push(next_info);
			}
		}
		Q.pop();
	}

	printf("%d\n",minimum);
}

int main(){

	makeMAP();

	while(true){
		scanf("%d %d",&cost_yoko,&cost_tate);
		if(cost_yoko == 0 && cost_tate == 0)break;

		func();
	}

	return 0;
}

Measured memory_bytes: 27024