class Heap
	require 'forwardable'
	extend Forwardable

	def initialize
		@array = []
		@index = {}
	end

	def add(obj, key)
		@array << [obj, key]
		@index[obj] = @array.size - 1
		lift_up(@array.size - 1)
	end

	def swap(i, j)
		u = @array[i]
		v = @array[j]
		@index[u[0]], @index[v[0]] = j, i
		@array[i], @array[j] = v, u
	end

	def lift_up(i)
		return if i == 0
		j = (i - 1) / 2
		if (@array[i][1] <=> @array[j][1]) < 0
			swap(i, j)
			lift_up(j)
		end
	end

	def del_min
		raise "heap is empty!" if empty?
		min = @array[0]
		if size > 1
			swap(0, @array.size-1)
			@array.pop
			lift_down(0)
		else
			@array.pop
		end
		@index.delete(min[0])
		min
	end

	def inc_key(obj, diff)
		i = @index[obj]
		@array[i][1] += diff
		lift_down(i)
	end

	def dec_key(obj, diff)
		i = @index[obj]
		@array[i][1] -= diff
		lift_up(i)
	end

	def update_key(obj, new_key)
		i = @index[obj]
		return unless i
		return if (new_key <=> @array[i][1]) >= 0
		@array[i][1] = new_key
		lift_up(i)
	end

	def min_obj
		@array[0][0]
	end

	def min_key
		@array[0][1]
	end

	def lift_down(i)
		j = 2 * i + 1
		k = j + 1
		if k >= size
			return if j >= size
			if (@array[i][1] <=> @array[j][1]) > 0
				swap(i, j)
				lift_down(j)
			end
		else
			l = ((@array[j][1] <=> @array[k][1]) < 0 ? j : k)
			if (@array[i][1] <=> @array[l][1]) > 0
				swap(i, l)
				lift_down(l)
			end
		end
	end

	def_delegators :@array, :empty?, :size
end

module Graphical
	require 'set'

	def bidir_dijkstra(s, t)
return 0 if s == t
		f_dist = {}
		b_dist = {}

		f_heap = Heap.new
		b_heap = Heap.new

		f_heap.add(s, 0)
		b_heap.add(t, 0)

		f_reached = Set[s]
		b_reached = Set[t]

		f_current_dist = b_current_dist = 0
		best = Float::INFINITY

		loop {
			if !f_heap.empty?
				u, key = f_heap.del_min
				f_dist[u] = key
				f_current_dist = key
				each_out_connection_of(u){|v, l|
					d = f_dist[u] + l
					if b_dist[v]
						path_len = d + b_dist[v]
						best = path_len if path_len < best
					end
					if f_reached.include?(v)
						f_heap.update_key(v, d)
					else
						f_heap.add(v, d)
						f_reached << v
					end
				}
			end

			if !b_heap.empty?
				u, key = b_heap.del_min
				b_dist[u] = key
				b_current_dist = key
				each_in_connection_of(u){|v, l|
					d = b_dist[u] + l
					if f_dist[v]
						path_len = d + f_dist[v]
						best = path_len if path_len < best
					end
					if b_reached.include?(v)
						b_heap.update_key(v, d)
					else
						b_heap.add(v, d)
						b_reached << v
					end
				}
			end

			return best if f_current_dist + b_current_dist >= best
			return nil if f_heap.empty? && b_heap.empty?
		}
	end
end

include Graphical

def each_out_connection_of(u)
	i = (0..2).find {|i| u[i].include?(0)}
	j = (0..2).find {|j| u[i][j] == 0}

	d = Marshal.dump(u)
	a = Marshal.load(d)
	if i > 0
		a[i][j], a[i-1][j] = a[i-1][j], a[i][j]
	else
		a[0][j], a[-1][j] = a[-1][j], a[0][j]
	end
	yield(a, $cv)

	a = Marshal.load(d)
	if i < 2
		a[i][j], a[i+1][j] = a[i+1][j], a[i][j]
	else
		a[0][j], a[-1][j] = a[-1][j], a[0][j]
	end
	yield(a, $cv)

	a = Marshal.load(d)
	if j > 0
		a[i][j], a[i][j-1] = a[i][j-1], a[i][j]
	else
		a[i][0], a[(i-1)%3][-1] = a[(i-1)%3][-1], a[i][0]
	end
	yield(a, $ch)

	a = Marshal.load(d)
	if j < 2
		a[i][j], a[i][j+1] = a[i][j+1], a[i][j]
	else
		a[(i+1)%3][0], a[i][-1] = a[i][-1], a[(i+1)%3][0]
	end
	yield(a, $ch)
end
alias :each_in_connection_of :each_out_connection_of

loop do
	$ch, $cv = gets.split.map(&:to_i)
	break if $ch == 0 && $cv == 0
	s = (1..3).map { gets.split.map(&:to_i) }
	g = (1..3).map { gets.split.map(&:to_i) }

	p bidir_dijkstra(s, g) 
end

Measured memory_bytes: 41184