#include "bits/stdc++.h"
//http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1155&lang=jp
using namespace std;
typedef long long ll;

int to_digit(int& at, string& S, int P, int Q, int R) {
	int ret;
	if (S[at] == 'P') ret = P;
	else if (S[at] == 'Q') ret = Q;
	else if (S[at] == 'R') ret = R;
	else ret = S[at] - '0';
	at++;
	return ret;
}
int dfs(int& at,string& S, int P, int Q, int R) {
	int ret;
	if (S[at] == '(') {
		at++;
		ret = dfs(at, S, P, Q, R);
		if (S[at] == '*') {
			at++;
			ret = min(ret, dfs(at, S, P, Q, R));
		}
		else if(S[at] == '+'){
			at++;
			ret = max(ret, dfs(at, S, P, Q, R));
		}

		//')'?????????
		at++;
		return ret;
	}
	else if (S[at] == '-') {
		at++;
		ret = dfs(at, S, P, Q, R);
		if (ret == 0) ret = 2;
		else if (ret == 1) ret = 1;
		else if (ret == 2) ret = 0;
		return ret;
	}
	else {
		return to_digit(at,S, P, Q, R);
	}
}

int main() {
	cin.tie(0); ios::sync_with_stdio(false);
	string S;
	while (cin >> S) {
		if (S == ".")break;
		int ans = 0;
		for (int P = 0; P < 3; P++) {
			for (int Q = 0; Q < 3;Q++) {
				for (int R = 0; R < 3;R++) {
					int at = 0;
					if (dfs(at, S, P, Q, R) == 2) ans++;
				}
			}
		}
		cout << ans << endl;
	}
}