### main

loop do
  $n, $m = gets.strip.split(' ').map{|s| s.to_i}
  break if $n == 0 && $m == 0

  target = []
  for k in (0..2)
    cs = gets.strip.split(' ').map{|s| s.to_i}
    cs.shift

    for c in cs
      target[$n - c] = k
    end
  end

  count0 = 0
  p = 0

  ($n - 1).downto(0) do |k|
    if p != target[k]
      d = (target[k] - p).abs
      count0 += d * (3 ** k)
      if d == 1
        p = 2 - p
      end
    end
  end

  count1 = (3 ** $n) - 1 - count0
  count = [count0, count1].min

  puts (count > $m ? -1 : count)
end