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

int Calc(string S, int P, int Q, int R) {
	//if (P == 0 && Q == 0 && R == 0) cout << S << endl;
	if (S[0] == '-') return 2 - Calc(S.substr(1, S.size() - 1), P, Q, R);
	if (S.size() == 1) {
		if (S[S.size() - 1] == 'P') return P;
		if (S[S.size() - 1] == 'Q') return Q;
		if (S[S.size() - 1] == 'R') return R;
		return S[S.size() - 1] - '0';
	}
	int Depth = 0;
	for (int i = 0; i < S.size(); i++) {
		if (Depth == 1 && S[i] == '+') return max(Calc(S.substr(1, i - 1), P, Q, R), Calc(S.substr(i + 1, S.size() - i - 2), P, Q, R));
		if (Depth == 1 && S[i] == '*') return min(Calc(S.substr(1, i - 1), P, Q, R), Calc(S.substr(i + 1, S.size() - i - 2), P, Q, R));
		if (S[i] == '(') Depth++;
		if (S[i] == ')') Depth--;
	}
}

int main() {
	string S;
	while (cin >> S) {
		if (S == ".") return 0;
		int ANS = 0;
		for (int P = 0; P < 3; P++) {
			for (int Q = 0; Q < 3; Q++) {
				for (int R = 0; R < 3; R++) {
					if (Calc(S, P, Q, R) == 2) ANS++;
				}
			}
		}
		cout << ANS << endl;
	}
}
