#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
string S, T; int pos;
int solve() {
	switch (T[pos++]) {
		case '0': return 0;
		case '1': return 1;
		case '2': return 2;
		case '-': return 2 - solve();
		case '(':
			int a = solve();
			switch (T[pos++]) {
				case '+': a = max(a, solve()); break;
				case '*': a = min(a, solve()); break;
			}
			pos++;
			return a;
	}
	return -1;
}
int main() {
	while (true) {
		cin >> S;
		if (S == ".") break;
		int ret = 0;
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 3; j++) {
				for (int k = 0; k < 3; k++) {
					T = S;
					for (int l = 0; l < S.size(); l++) {
						if (S[l] == 'P') T[l] = i + 48;
						if (S[l] == 'Q') T[l] = j + 48;
						if (S[l] == 'R') T[l] = k + 48;
					}
					pos = 0;
					if (solve() == 2) {
						ret++;
					}
				}
			}
		}
		printf("%d\n", ret);
	}
	return 0;
}