# -*- coding: utf-8 -*-
from itertools import product

class thee(int):
    def __add__(self, that):
        return thee(max(int(self), int(that)))

    def __mul__(self, that):
        return thee(min(int(self), int(that)))
    
    def __neg__(self):
        T = [2, 1, 0]
        return thee(T[int(self)])

S = input()
while S != ".":
    for x in list("012PQR"):
        S = S.replace(x, "thee({})".format(x))
    ans = 0
    for P, Q, R in product(range(3), repeat=3):
        ans += 2 == int(eval(S))
    print(ans)
    S = input()

