#!/usr/bin/env python

from collections import deque
import itertools as it
import sys

sys.setrecursionlimit(1000000)

while True:
    S_ = raw_input()
    if S_ == '.':
        break

    ans = 0
    for p, q, r in it.product([0, 1, 2], repeat = 3):
        S = S_
        S = S.replace("P", p).replace("Q", q).replace("R", r)
        
        for loop in range(50):
            for loop2 in range(100):
                S_pre = S
                S = S.replace("-0", "2")
                S = S.replace("-1", "1")
                S = S.replace("-2", "0")
                if S == S_pre:
                    break
            for loop2 in range(50):
                S_pre = S
                for a, b in it.product([0, 1, 2], repeat = 2):
                    S = S.replace("(%d*%d)" % (x, y), str(min(x, y)))
                    S = S.replace("(%d+%d)" % (x, y), str(max(x, y)))
                if S == S_pre:
                    break
        if S == "2":
            ans += 1
    print ans
