import scala.annotation.tailrec
import scala.collection.mutable
import scala.io.StdIn._
object Main extends App {

  val all = (1 << 16) - 1
  val minDistance = Array.fill(1 << 16){17}
  val byLength = Array.fill(17){Nil: List[Int]}
  val queue = mutable.PriorityQueue[(Int, Int)]()(Ordering.by[(Int, Int), Int](_._2).reverse)
  minDistance(0) = 1
  minDistance(all) = 1
  for (i ← 0 until 4) {
    var target = 0
    for (j ← 0 until 16 if (j & (1 << i)) != 0) {
      target |= 1 << j
    }
    minDistance(target) = 1
    byLength(1) ::= target
    queue.enqueue((target, 1))
  }
  while (queue.nonEmpty) {
    val (pat, step) = queue.dequeue()
    if (step == minDistance(pat)) {
      if (minDistance(~pat & all) > step + 1) {
        minDistance(~pat & all) = step + 1
        byLength(step + 1) ::= ~pat & all
        queue.enqueue((~pat & all, step + 1))
      }
      for (s ← 1 to step if s + step + 3 <= 16) {
        for (o ← byLength(s)) {
          if (minDistance(o & pat) > step + s + 3) {
            minDistance(o & pat) = step + s + 3
            queue.enqueue((o & pat, step + s + 3))
            byLength(step + s + 3) ::= o & pat
          }
          if (minDistance(o ^ pat) > step + s + 3) {
            minDistance(o ^ pat) = step + s + 3
            queue.enqueue((o ^ pat, step + s + 3))
            byLength(step + s + 3) ::= o ^ pat
          }
        }
      }
    }
  }
  solve()
  @tailrec def solve(): Unit = {
    val str = readLine.trim
    if (str == ".") return
    str match {
      case Expression(e) ⇒
        var target = 0
        for (i ← 0 until 16 if e.calculate(i)) {
          target |= 1 << i
        }
        println(minDistance(target))
      case _ ⇒ ???
    }
    solve()
  }

  implicit class Scope[T](val value: T) extends AnyVal {
    def let[A](@inline func: T ⇒ A): A = func(value)
    def also(@inline func: T ⇒ Unit): T = {func(value);value}
  }
}
sealed trait Expression {
  def calculate(state: Int): Boolean
  def not: Expression = Not(this)
  def and(that: Expression): Expression = And(this, that)
  def xor(that: Expression): Expression = Xor(this, that)
}
case class Not(expression: Expression) extends Expression {
  override def calculate(state: Int): Boolean = !expression.calculate(state)
}
case class And(left: Expression, right: Expression) extends Expression {
  override def calculate(state: Int): Boolean = left.calculate(state) && right.calculate(state)
}
case class Xor(left: Expression, right: Expression) extends Expression {
  override def calculate(state: Int): Boolean = left.calculate(state) ^ right.calculate(state)
}
case class Variable(val variable: Int) extends Expression {
  override def calculate(state: Int): Boolean = (state & (1 << variable)) != 0
}
object True extends Expression {
  override def calculate(state: Int): Boolean = true
}
object False extends Expression {
  override def calculate(state: Int): Boolean = false
}
object Expression {
  def unapply(expression: String): Option[Expression] = {
    unapply(expression.toList)
  }
  def unapply(args: List[Char]): Option[Expression] = {
    args match {
      case e Term Nil ⇒ Some(e)
      case _ ⇒ None
    }
  }
  object Term {
    def unapply(args: List[Char]): Option[(Expression, List[Char])] = {
      args match {
        case '0'::rest ⇒ Some(False, rest)
        case '1'::rest ⇒ Some(True, rest)
        case 'a'::rest ⇒ Some(Variable(0), rest)
        case 'b'::rest ⇒ Some(Variable(1), rest)
        case 'c'::rest ⇒ Some(Variable(2), rest)
        case 'd'::rest ⇒ Some(Variable(3), rest)
        case '(':: (left Term '*':: (right Term ')'::rest)) ⇒ Some(And(left, right), rest)
        case '(':: (left Term '^':: (right Term ')'::rest)) ⇒ Some(Xor(left, right), rest)
        case '-'::(term Term rest) ⇒ Some(Not(term), rest)
        case _ ⇒ None
      }
    }
  }
}


Measured memory_bytes: 49868