/*
 * Copyright (C) 2013  Hiroyasu Kamo
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/*
 * ACM-ICPC 2013 Asia Regional Contest, Aizu
 *  Problem E  Dragon's Cruller
 *
 *	Author: Hiroyasu Kamo <wd@ics.nara-wu.ac.jp>
 */

#include <algorithm>
#include <vector>
#include <map>
#include <queue>
#include <iterator>
#include <iostream>
#include <functional>
#include <utility>
#include <array>

using std::min;
using std::next_permutation;
using std::vector;
using std::map;
using std::priority_queue;
using std::distance;
using std::find;
using std::swap;
using std::greater;
using std::binary_function;
using std::pair;
using std::make_pair;
using std::istream;
using std::cin;
using std::cout;
using std::array;

#ifndef SIZE
#define SIZE 3
#endif

class mvcost {
private:
	int	vertical, horizontal;
public:
	int	right() const {return horizontal;}
	int	left() const {return horizontal;}
	int	up() const {return vertical;}
	int	down() const {return vertical;}
	int	min() const {return std::min(horizontal, vertical);}
	bool	ok() const {return horizontal != 0 && vertical != 0;}
	friend istream	&operator>>(istream &, mvcost &);
};
typedef array<unsigned char, SIZE * SIZE>	board;

inline istream &
operator>>(istream &is, board &bd)
{
	for (int i = 0; i < SIZE * SIZE; i ++) {
		int	piece;
		is >> piece;
		bd[i] = piece;
	}
	return is;
}

inline istream &
operator>>(istream &is, mvcost &costs)
{
	return is >> costs.horizontal >> costs.vertical;
}

inline const board
move_hole(const board &bd0, unsigned int d)
{
	board	bd = bd0;
	const unsigned int	hole = distance(bd.begin(), find(bd.begin(), bd.end(), 0));
	const unsigned int	pos = (hole + d) % (SIZE * SIZE);
	swap(bd[hole], bd[pos]);
	return bd;
}

const board
right_hole(const board &bd0)
{
	return move_hole(bd0, 1);
}

const board
down_hole(const board &bd0)
{
	return move_hole(bd0, SIZE);
}

const board
up_hole(const board &bd0)
{
	return move_hole(bd0, SIZE * (SIZE - 1));
}

const board
left_hole(const board &bd0)
{
	return move_hole(bd0, SIZE * SIZE - 1);
}

typedef pair<int, pair<board, int> >	element_t;
struct element_nearer : public binary_function<element_t, element_t, bool> {
	bool
	operator()(const element_t &a, const element_t &b)
	{
		return b.first < a.first;
	}
};

int
count_steps(const mvcost &costs, const board &init, const board &goal)
{
	priority_queue<element_t, vector<element_t>, element_nearer>	to_visit;
	to_visit.push(make_pair(0, make_pair(init, 0)));
	map<board, int>	visited;
	do {
		const pair<board, int>	v_front = to_visit.top().second;
		if (v_front.first == goal)
			return v_front.second;
		to_visit.pop();
		map<board, int>::iterator	r = visited.lower_bound(v_front.first);
		if (r == visited.end() || v_front.first < r->first) {
			visited.insert(r, v_front);
			const pair<board, int>	next[4] = {
				make_pair(right_hole(v_front.first), v_front.second + costs.right()),
				make_pair(down_hole(v_front.first), v_front.second + costs.down()),
				make_pair(up_hole(v_front.first), v_front.second + costs.up()),
				make_pair(left_hole(v_front.first), v_front.second + costs.left()),
			};
			for (const pair<board, int> *p = next; p != &next[4]; ++ p) {
				to_visit.push(make_pair(p->second, *p));
			}
		}
	} while (!to_visit.empty());
	return -1;
}

int
main(int argc, char **argv)
{
	mvcost	costs;
	board	init, goal;
	while (cin >> costs && costs.ok()) {
		cin >> init >> goal;
		cout << count_steps(costs, init, goal) << "\n";
	}
	return 0;
}
