#include <bits/stdc++.h>
using namespace std;

#define for_(i,a,b) for(int i=(a);i<(b);++i)

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

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

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

string str;
int var[3];

int parse(int l, int r) {
	int arc = 0, nl = -1;
	stack< char > op;
	stack< int > val;
	
	for_(i,l,r) {
		if (arc == 0 && str[i] == '(') {
			++arc;
			nl = i + 1;
		} else if (arc > 0) {
			if (str[i] == ')') {
				--arc;
				if (arc == 0) val.push(parse(nl, i));
			} else if (str[i] == '(') {
				++arc;
			}
		} else if (arc == 0) {
			if (isdigit(str[i])) val.push(str[i] - '0');
			else if (str[i] == 'P') val.push(var[0]);
			else if (str[i] == 'Q') val.push(var[1]);
			else if (str[i] == 'R') val.push(var[2]);
			else op.push(str[i]);
		}
	}
	
	if (val.size() == 1) {
		int res = val.top(); val.pop();
		
		while (!op.empty() && op.top() == '-') {
			op.pop();
			res = bar[res];
		}
		
		return res;
	}
	
	int x2 = val.top(); val.pop();
	while (op.top() == '-') {
		op.pop();
		x2 = bar[x2];
	}
	
	char opp = op.top(); op.pop();
	
	int x1 = val.top(); val.pop();
	while (!op.empty() && op.top() == '-') {
		op.pop();
		x1 = bar[x1];
	}
	
	return (opp == '*' ? mul[x1][x2] : add[x1][x2]);
}

int main() {
	while (cin >> str) {
		if (str == ".") break;
		
		int ans = 0;
		
		for_(p,0,3) for_(q,0,3) for_(r,0,3) {
			var[0] = p; var[1] = q; var[2] = r;
			if (parse(0, (int)str.size()) == 2) ++ans;
		}
		
		cout << ans << endl;
	}
}