object Main extends App {

  import scala.io.StdIn._
  solve
  def solve: Unit = {
    val line = readLine.trim
    if (line == ".") return
    line.toList match {
      case b Bool Nil ⇒ println(Combination.all.count(c ⇒ b(c) == True))
      case _ ⇒ ???
    }
    solve
  }
  object Combination {
    def all: List[Combination] = List.tabulate(3){_ ⇒ List(True, False, Unknown)}.foldLeft(Nil::Nil:List[List[Bool]]){(l, r) ⇒ l.flatMap(list ⇒ r.map(_::list))}.map{case p::q::r::Nil ⇒ Combination(p, q, r); case _ ⇒ ???}
  }
  case class Combination(p: Bool, q: Bool, r: Bool)
  sealed trait Bool {
    def unary_- : Bool
    def *(that: Bool): Bool
    def +(that: Bool): Bool
  }
  object Bool {
    def unapply(arg: List[Char]): Option[(Combination ⇒ Bool, List[Char])] = {
      arg match {
        case l Term rest ⇒ rest match {
          case Nil ⇒ Some(l, Nil)
          case '+'::(r Term t) ⇒ Some(c ⇒ l(c) + r(c), t)
          case '*'::(r Term t) ⇒ Some(c ⇒ l(c) * r(c), t)
          case _ ⇒ None
        }
        case _ ⇒ None
      }
    }
  }

  object True extends Bool {
    def unapply(arg: List[Char]): Option[(Bool, List[Char])] = {
      arg match {
        case '2'::t ⇒ Some(True, t)
        case _ ⇒ None
      }
    }
    override def unary_- : Bool = False
    override def * (that: Bool): Bool = that
    override def + (that: Bool): Bool = True
  }
  object False extends Bool {
    def unapply(arg: List[Char]): Option[(Bool, List[Char])] = {
      arg match {
        case '0'::t ⇒ Some(False, t)
        case _ ⇒ None
      }
    }
    override def unary_- : Bool = True
    override def * (that: Bool): Bool = False
    override def + (that: Bool): Bool = that
  }
  object Unknown extends Bool {
    def unapply(arg: List[Char]): Option[(Bool, List[Char])] = {
      arg match {
        case '1'::t ⇒ Some(Unknown, t)
        case _ ⇒ None
      }
    }
    override def unary_- : Bool = Unknown
    override def * (that: Bool): Bool = that match {
      case False ⇒ False
      case _ ⇒ Unknown
    }
    override def + (that: Bool): Bool = that match {
      case True ⇒ True
      case _ ⇒ Unknown
    }
  }
  object Variable {
    def unapply(arg: List[Char]): Option[(Combination ⇒ Bool, List[Char])] = {
      arg match {
        case 'P'::t ⇒ Some(_.p, t)
        case 'Q'::t ⇒ Some(_.q, t)
        case 'R'::t ⇒ Some(_.r, t)
        case _ ⇒ None
      }
    }
  }
  object Term {
    def unapply(arg: List[Char]): Option[(Combination ⇒ Bool, List[Char])] = {
      arg match {
        case '-'::(b Term t) ⇒ Some(c ⇒ -b(c), t)
        case '('::(b Bool ')'::t) ⇒ Some(b, t)
        case b True t ⇒ Some(_ ⇒ b, t)
        case b False t ⇒ Some(_ ⇒ b, t)
        case b Unknown t ⇒ Some(_ ⇒ b, t)
        case v Variable t ⇒ Some(v, t)
        case _ ⇒ None
      }
    }
  }
  implicit class Extension[T](val value :T) extends AnyVal {
    @inline
    def let[A](@inline func: T ⇒ A): A = func(value)
    @inline
    def also(@inline func: T ⇒ Unit): T = {func(value); value}
  }
}
