#include <stdio.h>
#include <string.h>

//FILE *in = freopen("input.txt", "r", stdin);

int n;
char data[100];

int mul[3][3] = {{0, 0, 0},{0, 1, 1},{0, 1, 2}};
int plu[3][3] = {{0, 1, 2},{1, 1, 2},{2, 2, 2}};

int P, Q, R;
int pos;

int back() {
	switch(data[pos++]) {
	case '0':
		return 0;
		break;
	case '1':
		return 1;
		break;
	case '2':
		return 2;
		break;
	case 'P':
		return P;
		break;
	case 'Q':
		return Q;
		break;
	case 'R':
		return R;
		break;
	case '-':
		return 2-back();
		break;
	case '(':
		int A = back();
		char c = data[pos++];
		if(c == ')')
			return A;
		int B = back();
		if(data[pos] == ')') {
			pos++;
			switch(c){
			case '*':
				return mul[A][B];
				break;
			case '+':
				return plu[A][B];
				break;
			default:
				printf("=_=;\n");
				break;
			}
		}
		else{
			printf("?_?\n");
		}
		break;
	}
	return -1;
}

void process() {
	int cnt = 0;
	for(P = 0; P <= 2; P++) {
		for(Q = 0; Q <= 2; Q++) {
			for(R = 0; R <= 2; R++) {
				pos = 0;
				int res = back();
				if(res == 2) {
					++cnt;
				}
			}
		}
	}
	printf("%d\n", cnt);
}

int main() {
	data[0] = '(';
	while(true){
		gets(data+1);
		n = strlen(data) + 1;
		data[n-1] = ')';
		data[n] = 0;
		if(data[1] == '.') break;
		process();
	}
	return 0;
}