#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
string S, T; int pos;
int solve() {
	if (T[pos] == '0') { pos++; return 0; }
	if (T[pos] == '1') { pos++; return 1; }
	if (T[pos] == '2') { pos++; return 2; }
	if (T[pos] == '-') { pos++; return 2 - solve(); }
	if (T[pos] == '(') {
		pos++; int a = solve(); char c = T[pos];
		pos++; int b = solve(); pos++;
		if (c == '+') return max(a, b);
		if (c == '*') return min(a, b);
	}
	return 0;
}
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;
}