module main;

import std.array : array, empty, back;
import std.algorithm : map, filter, schwartzSort;
import std.conv : to, ConvException;
import std.range : zip, iota, drop;
import std.stdio : stdin, readln, writeln;
import std.string : split;
import std.typecons : Nullable, Tuple;

auto input()
{
	return stdin.readln().split().map!(to!int);
}

struct Problem
{
	alias int[] Tray;
	alias Tray[3] Trays;
	
	int cup_count, swap_limit;
	Trays trays;
	
	public void solve()
	{
		auto result = dive(0, trays);
		debug writeln(result);
		writeln(result.swap_count > swap_limit ? -1 : result.swap_count);
	}
	
	alias Dive = Tuple!(int, "swap_count", Trays, "trays");
	
	private Dive dive(int swap_count, Trays trays)
	{
		return dive(Dive(swap_count, trays));
	}
	
	private Dive dive(Dive state)
	{
		if (state.swap_count > swap_limit)
		{
			return state;
		}
		
		const lesser_and_max = top2(state.trays);
		const lesser = lesser_and_max[0];
		const max = lesser_and_max[1];
		
		assert(!max.isNull);
		assert(lesser.isNull || lesser != max);
		
		if (lesser.isNull)
		{
			if (max == TrayID.B)
			{
				Dive m = move(max, TrayID.A, state.trays);
				return Dive(state.swap_count + m.swap_count, m.trays);
			}
			else
			{
				return state;
			}
		}
		
		const other = [TrayID.A, TrayID.B, TrayID.C].filter!(x => x != max && x != lesser).front;
		
		if (max == TrayID.B)
		{
			auto d1 = move(max, other, state.trays);
			auto d2 = move1(lesser, max, d1.trays);
			auto way1 = dive(state.swap_count + d1.swap_count + d2.swap_count, d2.trays);
			
			auto d3 = move(max, lesser, state.trays);
			auto way2 = dive(state.swap_count + d3.swap_count, d3.trays);
			
			return way1.swap_count < way2.swap_count ? way1 : way2;
		}
		else
		{
			auto d1 = move1(lesser, other, state.trays);
			auto d2 = move(max, other, d1.trays);
			auto way1 = dive(state.swap_count + d1.swap_count + d2.swap_count, d2.trays);
			
			auto d3 = move(max, lesser, state.trays);
			auto way2 = dive(state.swap_count + d3.swap_count, d3.trays);
			
			return way1.swap_count < way2.swap_count ? way1 : way2;
		}
	}
	
	enum TrayID { A, B, C };
	alias Nullable!TrayID[2] Top2;
	
	private Top2 top2(Trays trays)
	{
		auto pairs = zip(trays[], [TrayID.A, TrayID.B, TrayID.C]);
		auto sorted = pairs.schwartzSort!(pair => pair[0].empty ? -1 : pair[0].back);
		auto indices = sorted.map!(pair => pair[0].empty ? Nullable!TrayID() : Nullable!TrayID(pair[1]));
		return cast(Top2)indices.drop(1).array()[0..2];
	}
	
	private pure Dive move1(TrayID from, TrayID to, Trays trays)
		in
	{
		assert(from != to, "source and destination must differ");
		assert(!trays[from].empty);
	}
	body
	{
		debug writeln("move1 ", from, ",", to, ", ", trays);
		
		Trays t = trays.dup;
		t[to] = trays[to] ~ t[from].back;
		t[from] = trays[from][0..$ - 1];
		
		const steps = to == TrayID.B || from == TrayID.B ? 1 : 2;
		
		return Dive(steps, t);
	}
	
	private pure Dive move(TrayID from, TrayID to, Trays trays)
		in
	{
		assert(from != to, "source and destination must differ");
		assert(!trays[from].empty);
	}
	body
	{
		debug writeln("move ", from, ",", to, ", ", trays);
		
		if (trays[from].length == 1)
		{
			return move1(from, to, trays);
		}
		
		uint i = trays[from].length - 1;
		while (i > 0 && trays[from][i - 1] == trays[from][i] - 1)
		{
			i--;
		}
		
		Trays t = trays.dup;
		t[to] = t[to] ~ t[from][i..$];
		t[from] = t[from][0..i];
		
		const steps = (3 ^^ (trays[from].length - i) - 1) / (to == TrayID.B || from == TrayID.B ? 2 : 1);
		
		return Dive(steps, t);
	}
	
	public static Nullable!Problem read()
	{
		try
		{
			auto a = input();
			if (a.length != 2 || a[0] == 0 && a[1] == 0)
			{
				return Nullable!Problem();
			}
			int[][3] trays = iota(3).map!(_ => input().drop(1).array()).array();
			return Nullable!Problem(Problem(a[0], a[1], trays));
		}
		catch (ConvException)
		{
			return Nullable!Problem();
		}
	}
}

void main(string[] args)
{
	Nullable!Problem problem;
	while (!(problem = Problem.read()).isNull)
	{
		problem.solve();
	}
}