// Aizu 1155: How can I satisfy thee? Let me count the...
// 2017.9.13 bal4u@uu

#include <stdio.h>
#include <ctype.h>

char buf[100], *bp;
int p, q, r;

int term();

int factor()
{
	int x;

	if      (*bp == '(') bp++, x = term(), bp++;
	else if (*bp == '-') bp++, x = 2-factor();
	else if (isdigit(*bp)) x = *bp++ - '0';
	else if (*bp == 'P') bp++, x = p;
	else if (*bp == 'Q') bp++, x = q;
	else bp++, x = r;
	return x;
}

int term()
{
	int x, y, op;

	x = factor();
	if      (*bp == '*') bp++, op = 1;
	else if (*bp == '+') bp++, op = 0;
	y = factor();
	if (op) {
		if (x == 2 && y == 2) x = 2;
		else if (x == 0 || y == 0) x = 0;
		else x = 1;
	} else {
		if (x == 0 && y == 0) x = 0;
		else if (x == 2 || y == 2) x = 2;
		else x = 1;
	}
	return x;
}

int main()
{
	int ans;

	while (scanf("%s", buf) && *buf != '.') {
		ans = 0;
		for (p = 0; p <= 2; p++) for (q = 0; q <= 2; q++) for (r = 0; r <= 2; r++) {
			bp = buf; if (factor() == 2) ans++;
		}
		printf("%d\n", ans);
	}
	return 0;
}