import itertools
s = ""
while True:
	s = raw_input()
	if s == ".": break
	count = 0
	for p, q, r in itertools.product(xrange(3), repeat=3):
		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)))
		if f == "2":
			count += 1
	print count