class F:
 __neg__=lambda s:F(2-s.v)
 __add__=lambda s,t:F(max(s.v, t.v))
 __mul__=lambda s,t:F(min(s.v, t.v))
 def __init__(s,v):s.v = v
import itertools as i,re
while 1:
 s=raw_input()
 if s==".":break
 s=re.sub("([0-R])","F(\\1)",s)
 print sum(eval(s).v==2 for P,Q,R in i.product([0,1,2],repeat=3))