using System;
using System.Collections.Generic;

// http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2556344#3

public class CupState
{
	
	public byte[] A;
	public byte[] B;
	public byte[] C;
	public List<byte> Method;
	public byte LastM;
}


public class Test
{
	public static void Main()
	{
		while(true){
		  string[] str1 = Console.ReadLine().Split(' ');
		  int n = int.Parse(str1[0]); 
		  int m = int.Parse(str1[1]); 

		  if(n == 0) break;
		
		  int[] a = Array.ConvertAll(Console.ReadLine().Split(' '), new Converter<string,int>(s => int.Parse(s)));
		  int[] b = Array.ConvertAll(Console.ReadLine().Split(' '), new Converter<string,int>(s => int.Parse(s)));
		  int[] c = Array.ConvertAll(Console.ReadLine().Split(' '), new Converter<string,int>(s => int.Parse(s)));
		  //Console.WriteLine("???????????? N = {0} P = {1} Ai = {2}", n, p, a);
		  Console.WriteLine(calc(n,m,a,b,c));
		
		
		}

	}

  public static int calc(int n,int m,int[] a,int[] b,int[] c) {
	  Stack<byte> aa = new Stack<byte>();
	  for(int i=1;i<a.Length;i++) aa.Push((byte)a[i]);
	  Stack<byte> bb = new Stack<byte>();
	  for(int i=1;i<b.Length;i++) bb.Push((byte)b[i]);
	  Stack<byte> cc = new Stack<byte>();
	  for(int i=1;i<c.Length;i++) cc.Push((byte)c[i]);
	  
	  byte[] aaa = new byte[a.Length-1];
	  for(int i=1;i<a.Length;i++) aaa[i-1] = (byte)a[i];
	  byte[] bbb = new byte[b.Length-1];
	  for(int i=1;i<b.Length;i++) bbb[i-1] = (byte)b[i];
	  byte[] ccc = new byte[c.Length-1];
	  for(int i=1;i<c.Length;i++) ccc[i-1] = (byte)c[i];
	  
	  //Console.WriteLine("A = {0} B = {1} C = {2}", String.Join("", aa), String.Join("", bb), String.Join("", cc));
/*
	  List<byte[][]> met = new List<byte[][]>();
	  met.Add(new byte[][]{aa.ToArray(),bb.ToArray(),cc.ToArray()});
	  
	  int t = calc_sub_dbg(0, m, aa, bb, cc, met);

	  foreach(byte[][] d in met) {
	    Console.WriteLine("==========");
	    foreach(byte[] e in d) Console.WriteLine(String.Join(" ", e));
	  }
	  Console.WriteLine("==========");
*/

	  int t = calc_sub_2(0, m, aaa, bbb, ccc);


	  return t;
  }
  
  
  public static int calc_sub_2(int cnt,int max,byte[] a,byte[] b,byte[] c) {
	
	List<CupState> mcsl = new List<CupState>();
	CupState cs = new CupState();
	cs.A = a; cs.B = b; cs.C = c; cs.Method = new List<byte>(); cs.LastM = 0;
	mcsl.Add(cs);
	List<CupState> ccsl = new List<CupState>();

	int mincnt = max;
	for(int i=0;i<=max;i++){
	  if(mcsl.Count == 0) break;
	  List<CupState> acsl = new List<CupState>();
      foreach(CupState ccs in mcsl){
		//Console.WriteLine(String.Join(" ", ccs.Method.ToArray()));
		
        if ((ccs.B.Length == 0) && ((ccs.A.Length == 0) || (ccs.C.Length == 0))){
          //if(ccs.Method.Count < mincnt) mincnt = ccs.Method.Count;
          if(i < mincnt) mincnt = i;
          ccsl.Add(ccs);
          continue;
        }
        //if (mincnt < ccs.Method.Count){
        //  continue;
        //}
        
        
        //int mode = (ccs.Method.Count==0) ? 0 : (int)ccs.Method[ccs.Method.Count-1];
        int va = (ccs.A.Length==0) ? -1 : (int)ccs.A[ccs.A.Length-1];
        int vb = (ccs.B.Length==0) ? -1 : (int)ccs.B[ccs.B.Length-1];
        int vc = (ccs.C.Length==0) ? -1 : (int)ccs.C[ccs.C.Length-1];
        
        //a->b
	    if((ccs.LastM != 2) && (vb < va)) {
	      CupState csa = new CupState();
          csa.A = new byte[ccs.A.Length-1];
          Array.Copy(ccs.A,0,csa.A,0,ccs.A.Length-1);
          csa.B = new byte[ccs.B.Length+1];
          Array.Copy(ccs.B,0,csa.B,0,ccs.B.Length);
          csa.B[csa.B.Length-1] = ccs.A[ccs.A.Length-1];
          csa.C = ccs.C;
          //csa.Method = new List<byte>(ccs.Method.ToArray());
          //csa.Method.Add(1);
          csa.LastM = 1;
          acsl.Add(csa);
	    }
        //b->a
	    if((ccs.LastM != 1) && (va < vb)) {
	      CupState csa = new CupState();
          csa.B = new byte[ccs.B.Length-1];
          Array.Copy(ccs.B,0,csa.B,0,ccs.B.Length-1);
          csa.A = new byte[ccs.A.Length+1];
          Array.Copy(ccs.A,0,csa.A,0,ccs.A.Length);
          csa.A[csa.A.Length-1] = ccs.B[ccs.B.Length-1];
          csa.C = ccs.C;
          //csa.Method = new List<byte>(ccs.Method.ToArray());
          //csa.Method.Add(2);
          csa.LastM = 2;
          acsl.Add(csa);
	    }
        //b->c
	    if((ccs.LastM != 4) && (vc < vb)) {
	      CupState csa = new CupState();
          csa.B = new byte[ccs.B.Length-1];
          Array.Copy(ccs.B,0,csa.B,0,ccs.B.Length-1);
          csa.C = new byte[ccs.C.Length+1];
          Array.Copy(ccs.C,0,csa.C,0,ccs.C.Length);
          csa.C[csa.C.Length-1] = ccs.B[ccs.B.Length-1];
          csa.A = ccs.A;
          //csa.Method = new List<byte>(ccs.Method.ToArray());
          //csa.Method.Add(3);
          csa.LastM = 3;
          acsl.Add(csa);
	    }
        //c->b
	    if((ccs.LastM != 3) && (vb < vc)) {
	      CupState csa = new CupState();
          csa.C = new byte[ccs.C.Length-1];
          Array.Copy(ccs.C,0,csa.C,0,ccs.C.Length-1);
          csa.B = new byte[ccs.B.Length+1];
          Array.Copy(ccs.B,0,csa.B,0,ccs.B.Length);
          csa.B[csa.B.Length-1] = ccs.C[ccs.C.Length-1];
          csa.A = ccs.A;
          //csa.Method = new List<byte>(ccs.Method.ToArray());
          //csa.Method.Add(4);
          csa.LastM = 4;
          acsl.Add(csa);
	    }
	  }
      mcsl = acsl;
      
	}
	
	return (ccsl.Count == 0) ? -1 : mincnt;
  }
  
  
  public static int calc_sub_1(int cnt,int max,Stack<byte>a,Stack<byte> b,Stack<byte> c, byte mode) {
	  if((a.Count==0) && (b.Count==0)) return cnt;
	  if((b.Count==0) && (c.Count==0)) return cnt;
	  if(max<cnt) return -1;
	  
	  int mincnt = max;
	  bool suc = false;
	  
	  //a->b
	  if((mode != 2) && (0<a.Count) && ((b.Count==0) || (b.Peek() < a.Peek()))) {
	    b.Push(a.Pop());
		int t = calc_sub_1(cnt+1, mincnt, a, b, c, 1);
		a.Push(b.Pop());
		if ((0<=t)&&(t<mincnt)) mincnt = t;
		if(0<=t) suc = true;
	  }
	  
	  //b->a
	  if((mode != 1) && (0<b.Count) && ((a.Count==0) || (a.Peek() < b.Peek()))) {
	    a.Push(b.Pop());
		int t = calc_sub_1(cnt+1, mincnt, a, b, c, 2);
		b.Push(a.Pop());
		if ((0<=t)&&(t<mincnt)) mincnt = t;
		if(0<=t) suc = true;
	  }
	  
	  //b->c
	  if((mode != 4) && (0<b.Count) && ((c.Count==0) || (c.Peek() < b.Peek()))) {
	    c.Push(b.Pop());
		int t = calc_sub_1(cnt+1, mincnt, a, b, c, 3);
		b.Push(c.Pop());
		if ((0<=t)&&(t<mincnt)) mincnt = t;
		if(0<=t) suc = true;
	  }
	  
	  //c->b
	  if((mode != 3) && (0<c.Count) && ((b.Count==0) || (b.Peek() < c.Peek()))) {
	    b.Push(c.Pop());
		int t = calc_sub_1(cnt+1, mincnt, a, b, c, 4);
		c.Push(b.Pop());
		if ((0<=t)&&(t<mincnt)) mincnt = t;
		if(0<=t) suc = true;
	  }
	  
	  if(suc) {
	    return mincnt;
	  } else {
		return -1;
	  }

  }
  
  public static int calc_sub_dbg(int cnt,int max,Stack<byte>a,Stack<byte> b,Stack<byte> c, List<byte[][]> met) {
	  if((a.Count==0) && (b.Count==0)) return cnt;
	  if((b.Count==0) && (c.Count==0)) return cnt;
	  if(max<cnt) return max+10;
	  
	  int mincnt = max+10;
	  List<byte[][]> m1 = new List<byte[][]>();
	  List<byte[][]> m2 = new List<byte[][]>();
	  List<byte[][]> m3 = new List<byte[][]>();
	  List<byte[][]> m4 = new List<byte[][]>();
	  int t1 = mincnt;
	  int t2 = mincnt;
	  int t3 = mincnt;
	  int t4 = mincnt;
	  
	  //a->b
	  if((0<a.Count) && ((b.Count==0) || (b.Peek() < a.Peek()))) {
	    Stack<byte> aa = CloneStack<byte>(a);
	    Stack<byte> bb = CloneStack<byte>(b);
	    bb.Push(aa.Pop());

		//m1.Add(new byte[][]{aa.ToArray(),bb.ToArray(),c.ToArray()});
		t1 = calc_sub_dbg(cnt+1, max, aa, bb, c, m1);
	  }
	  
	  //b->a
	  if((0<b.Count) && ((a.Count==0) || (a.Peek() < b.Peek()))) {
	    Stack<byte> aa = CloneStack<byte>(a);
	    Stack<byte> bb = CloneStack<byte>(b);
	    aa.Push(bb.Pop());

		//m2.Add(new byte[][]{aa.ToArray(),bb.ToArray(),c.ToArray()});
		t2 = calc_sub_dbg(cnt+1, max, aa, bb, c, m2);
	  }
	  
	  //b->c
	  if((0<b.Count) && ((c.Count==0) || (c.Peek() < b.Peek()))) {
	    Stack<byte> bb = CloneStack<byte>(b);
	    Stack<byte> cc = CloneStack<byte>(c);
	    cc.Push(bb.Pop());

		//m3.Add(new byte[][]{a.ToArray(),bb.ToArray(),cc.ToArray()});
		t3 = calc_sub_dbg(cnt+1, max, a, bb, cc, m3);
	  }
	  
	  //c->b
	  if((0<c.Count) && ((b.Count==0) || (b.Peek() < c.Peek()))) {
	    Stack<byte> bb = CloneStack<byte>(b);
	    Stack<byte> cc = CloneStack<byte>(c);
	    bb.Push(cc.Pop());

		//m4.Add(new byte[][]{a.ToArray(),bb.ToArray(),cc.ToArray()});
		t4 = calc_sub_dbg(cnt+1, max, a, bb, cc, m4);
	  }
	  
	  mincnt = Math.Min(Math.Min(t1,t2),Math.Min(t3,t4));
	  if(t1==mincnt) { met.AddRange(m1); return mincnt; }
	  if(t2==mincnt) { met.AddRange(m2); return mincnt; }
	  if(t3==mincnt) { met.AddRange(m3); return mincnt; }
	  if(t4==mincnt) { met.AddRange(m4); return mincnt; }
	  
	  return mincnt;
  }
  
    public static Stack<T> CloneStack<T>(Stack<T> original)
    {
        var arr = new T[original.Count];
        original.CopyTo(arr, 0);
        Array.Reverse(arr);
        return new Stack<T>(arr);
    }
  
  
  
  
  
  
}

Measured memory_bytes: 10288