import itertools
s = ""
def satisfy(p, q, r):
	global s
	f = s.replace("P", str(p)).replace("Q", str(q)).replace("R", str(r))
	while len(f) > 1:
		f = f.replace("-0", "2").replace("-1", "1").replace("-2", "0")
		for x, y in itertools.product(xrange(3), repeat=2):
			f = f.replace("(%d*%d)" % (x, y), str(min(x, y)))
			f = f.replace("(%d+%d)" % (x, y), str(max(x, y)))
	return f == "2"
while True:
	s = raw_input()
	if s == ".": break
	count = 0
	for p, q, r in itertools.product(xrange(3), repeat=3):
		if satisfy(p, q, r):
			count += 1
	print count