#!/usr/bin/env python

from collections import deque
import sys

sys.setrecursionlimit(1000000)

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

    lst = []
    for i in range(3):
        for j in range(3):
            for k in range(3):
                lst.append((str(i), str(j), str(k)))
    
    ans = 0
    for p in lst:
        S = S_
        S = S.replace("P", p[0])
        S = S.replace("Q", p[1])
        S = S.replace("R", p[2])
        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
                S = S.replace("(0*0)", "0")
                S = S.replace("(0*1)", "0")
                S = S.replace("(0*2)", "0")
                S = S.replace("(1*0)", "0")
                S = S.replace("(1*1)", "1")
                S = S.replace("(1*2)", "1")
                S = S.replace("(2*0)", "0")
                S = S.replace("(2*1)", "1")
                S = S.replace("(2*2)", "2")
                S = S.replace("(0+0)", "0")
                S = S.replace("(0+1)", "1")
                S = S.replace("(0+2)", "2")
                S = S.replace("(1+0)", "1")
                S = S.replace("(1+1)", "1")
                S = S.replace("(1+2)", "2")
                S = S.replace("(2+0)", "2")
                S = S.replace("(2+1)", "2")
                S = S.replace("(2+2)", "2")
                if S == S_pre:
                    break
        if S == "2":
            ans += 1
    print ans
