Max = 1500000
A, B, C = (0...3).to_a
From, To = 1, 0

until (n,m=gets.split.map(&:to_i)).all?(&:zero?) do
  tray = Array.new
  3.times do
    tray.push([-1] + gets.split.slice(1...$_.split.size).map(&:to_i))
  end
  answer = Max + 1
  [[A,B], [B,C]].each do |move|
    t = tray.map(&:dup)
    for depth in 0..m do
      if t[A].size==n+1 || t[C].size==n+1
        answer = [answer, depth].min
        break
      end
      move.sort!{|a,b|t[a].last<=>t[b].last}
      t[move[To]].push( t[move[From]].pop )
      move = move.include?(C) ? [A,B] : [B,C]
    end
  end
  p answer<=Max ? answer : -1
end