object Main {
  import scala.io.StdIn.{ readLine }
  import scala.language.implicitConversions.{_}
  def main(args : Array[String]) {
    recursive
  }
  def recursive : Unit = {
    var args = readLine.split(" ").map(_.toInt)
    while (args(0) + args(1) != 0) {
      val a = toSeqList(readLine.split(" ").map(s => new Cup(s.toInt)).toList.tail)
      val b = toSeqList(readLine.split(" ").map(s => new Cup(s.toInt)).toList.tail)
      val c = toSeqList(readLine.split(" ").map(s => new Cup(s.toInt)).toList.tail)
      val min = minStep(a, b, c)
      if (args(1) >= min) {
        println(min)
      } else {
        println(-1)
      }
      args = readLine.split(" ").map(_.toInt)
    }
  }
  //implicit def boolToInt(b : Boolean) : Int = { if (b) 1 else 0 }
  def getOrder(a : Cup, b : Cup, c : Cup) = {
    ((a > b) + (a > c), (b > a) + (b > c), (c > a) + (c > b))
  }
  def toSeqList(list : List[Cup], result : List[Cup] = List[Cup](new Stage)) : List[Cup] = {
    list match {
      case Nil => result
      case first :: rest =>
        if (first ^ result.head)
          toSeqList(rest, (first + result.head) :: result.tail)
        else
          toSeqList(rest, first :: result)
      case _ => throw new Exception
    }
  }
  def xor(a : Boolean, b : Boolean) : Boolean = !(a == b)
  def subOneStep3(a : List[Cup], b : List[Cup], c : List[Cup]) = {
    getOrder(a.head, b.head, c.head) match {
      case (0, 1, 2) =>
        (a, (c.head + b.head) :: b.tail, c.tail, c.head.moveCost * (if (xor((c.tail.head > a.head), (c.head.parity == b.head.parity))) 1 else -1))
      case (2, 1, 0) =>
        (a.tail, (a.head + b.head) :: b.tail, c, a.head.moveCost * (if (xor((a.tail.head > c.head), (a.head.parity == b.head.parity))) 1 else -1))
      case (1, 2, 0) =>
        ((b.head + a.head) :: a.tail, b.tail, c, b.head.moveCost * (if (xor((b.tail.head > c.head), (b.head.parity == a.head.parity))) 1 else -1))
      case (0, 2, 1) =>
        (a, b.tail, (b.head + c.head) :: c.tail, b.head.moveCost * (if (xor((b.tail.head > a.head), (b.head.parity == c.head.parity))) 1 else -1))
      case (1, 0, 2) =>
        (a, (c.head + b.head) :: b.tail, c.tail, -c.head.moveCost)
      case (2, 0, 1) =>
        (a.tail, (a.head + b.head) :: b.tail, c, -a.head.moveCost)
      case _ => throw (new Exception)
    }
  }
  def subOneStep2(a : List[Cup], b : List[Cup], c : List[Cup]) = {
    getOrder(a.head, b.head, c.head) match {
      case (0, 1, 2) =>
        (a, (c.head + b.head) :: b.tail, c.tail, -c.head.moveCost)
      case (2, 1, 0) =>
        (a.tail, (a.head + b.head) :: b.tail, c, -a.head.moveCost)
      case (1, 2, 0) =>
        ((b.head + a.head) :: a.tail, b.tail, c, b.head.moveCost * (if (b.size == 2) 1 else -1))
      case (0, 2, 1) =>
        (a, b.tail, (b.head + c.head) :: c.tail, b.head.moveCost * (if (b.size == 2) 1 else -1))
      case (1, 0, 2) =>
        (a, (c.head + b.head) :: b.tail, c.tail, -c.head.moveCost)
      case (2, 0, 1) =>
        (a.tail, (a.head + b.head) :: b.tail, c, -a.head.moveCost)
      case _ => throw (new Exception)
    }
  }
  def minStep(a : List[Cup], b : List[Cup], c : List[Cup], step : Int = 0) : Int = {
    ((a.size > 1) + (b.size > 1) + (c.size > 1)) match {
      case 3 =>
        val tuple = subOneStep3(a, b, c)
        minStep(tuple._1, tuple._2, tuple._3, step + tuple._4)
      case 2 =>
        val tuple = subOneStep2(a, b, c)
        minStep(tuple._1, tuple._2, tuple._3, step + tuple._4)
      case 1 if (b.size > 1) =>
        step + b.head.moveCost
      case 1 =>
        step
      case _ => throw (new Exception)
    }
  }

}
class Cup(val bottom : Int, val top : Int) extends Ordered[Cup] {
  def this(s : Int) = this(s, s)
  lazy val size = top - bottom + 1
  override def compare(that : Cup) : Int = this.bottom - that.bottom
  def +(other : Cup) : Cup = {
    (this < other) match {
      case true  => new Cup(bottom, other.top)
      case false => new Cup(other.bottom, top)
    }
  }
  lazy val moveCost = (pow(top - bottom + 1, 1) - 1) / 2
  private lazy val pow : (Int, Int) => Int = (n, r) => {
    n match {
      case 0 => r
      case _ => pow(n - 1, 3 * r)
    }
  }
  def ^(other : Cup) : Boolean = (((other.bottom + 1) == top) || ((bottom + 1) == other.top))
  lazy val parity = bottom % 2
}
class Stage extends Cup(-1, -1) {
  override def ^(other : Cup) : Boolean = false
}