object Main {
  import scala.io.StdIn.{ readLine }
  def main(args : Array[String]) = {
    sub()
  }
  def sub(args : Array[Int] = readLine.split(" ").map(_.toInt)) {
    if ((args(0) != 0) || (args(1) != 0)) {
      val list = sort(readLine.split(" ").toList.map(x => new Cup(x.toInt, 0, args(0))).tail ::: readLine.split(" ").toList.map(x => new Cup(x.toInt, 1, args(0))).tail ::: readLine.split(" ").toList.map(x => new Cup(x.toInt, 2, args(0))).tail)
      val minStep = list.head.Pile match {
        case 1 =>
          min(moveEnd(list, 0), moveEnd(list, 2))
        case t : Int =>
          moveEnd(list, t)
      }
      if (minStep < args(1)) {
        println(minStep)
      } else {
        println(-1)
      }
      sub()
    }
  }
  def min(a : Int, b : Int) : Int = if (a > b) b else a
  def moveEnd(list : List[Cup], target : Int) : Int = {
    list match {
      case Nil => 0
      case first :: rest =>
        first match {
          case x @ Cup(_, t, _) if (t == target) =>
            moveEnd(rest, target)
          case x @ Cup(_, 1, _) =>
            moveEnd(rest, 2 - target) + x.moveCost * 2 + 1
          case x : Cup =>
            moveEnd(rest, target) + x.moveCost * 4 + 2
        }
    }
  }
  def insertSort(factor : Cup, list : List[Cup]) : List[Cup] = {
    list match {
      case Nil => List[Cup](factor)
      case first :: rest =>
        if (first < factor) {
          first :: insertSort(factor, rest)
        } else {
          factor :: list
        }
    }
  }
  def sort(ramlist : List[Cup], re : List[Cup] = Nil) : List[Cup] = {
    ramlist match {
      case Nil => re
      case first :: rest =>
        sort(rest, insertSort(first, re))
    }
  }
}
case class Cup(val Size : Int, val Pile : Int, val whole : Int) extends Ordered[Cup] {
  def compare(other : Cup) = Size - other.Size
  def moveCost : Int = {
    def pow(e : Int, r : Int = 1) : Int = {
      e match {
        case 0 => r
        case _ => pow(e - 1, 3 * r)
      }
    }
    (pow(whole - Size) - 1) / 2
  }
  override val toString = s"--Size = ${Size}, Pile = ${Pile}--"
}