#include <iostream>

#define rep(i,n) for(int i=0; i<(n); i++)

using namespace std;

typedef string::const_iterator Cursol;

int P, Q, R;
int op1[3] = {2, 1, 0};
int op2[3][3] = {{0, 0, 0}, {0, 1, 1}, {0, 1, 2}};
int op3[3][3] = {{0, 1, 2}, {1, 1, 2}, {2, 2, 2}};

int expr(Cursol &c) {
	int res = 0;
	if(*c == 'P') {
		res = P;
	} else if(*c == 'Q') {
		res = Q;
	} else if(*c == 'R') {
		res = R;
	} else if(isdigit(*c)) {
		res = *c - '0';
	} else if(*c == '-') {
		return op1[expr(++c)];
	} else if(*c == '(') {
		res = expr(++c);
		if(*c == '*') {
			res = op2[res][expr(++c)];
		} else if(*c == '+') {
			res = op3[res][expr(++c)];
		}
	}
	c++;
	return res;
}

int main() {
	string s;
	Cursol c;
	while(cin >> s) {
		if(s == ".") break;
		int ans = 0;
		for(P = 0; P < 3; P++) {
			for(Q = 0; Q < 3; Q++) {
				for(R = 0; R < 3; R++) {
					c = s.begin();
					if(expr(c) == 2) ans++;
				}
			}
		}
		cout << ans << endl;
	}
}