//たぶんO(NlogN)の構文解析
#include <iostream>
#include <string>
#define rep(i, n) for(i = 0; i < n; i++)
using namespace std;

int NOT[3] = {2, 1, 0};
int AND[3][3] = {{0, 0, 0}, {0, 1, 1}, {0, 1, 2}};
int OR[3][3] = {{0, 1, 2}, {1, 1, 2}, {2, 2, 2}};
string s;
int P, Q, R;

int dfs(int l, int r) {
	if (s[l] == '0') return 0;
	if (s[l] == '1') return 1;
	if (s[l] == '2') return 2;
	if (s[l] == 'P') return P;
	if (s[l] == 'Q') return Q;
	if (s[l] == 'R') return R;
	if (s[l] == '-') return NOT[dfs(l + 1, r)];
	
	int lc = l, cntl = 0;
	int rc = r - 1, cntr = 0;
	int it = -1;
	while (lc <= rc) {
		if (s[lc] == '(') cntl++;
		if (s[lc] == ')') cntl--;
		if (s[lc] == '+' && cntl == 1) { it = lc; break; }
		if (s[lc] == '*' && cntl == 1) { it = lc; break; }
		lc++;
		
		if (s[rc] == '(') cntr--;
		if (s[rc] == ')') cntr++;
		if (s[rc] == '+' && cntr == 1) { it = rc; break; }
		if (s[rc] == '*' && cntr == 1) { it = rc; break; }
		rc--;
	}
	
	int res1 = dfs(l + 1, it);
	int res2 = dfs(it + 1, r - 1);
	if (s[it] == '*') return AND[res1][res2];
	return OR[res1][res2];
}

int main() {
	while (cin >> s) {
		if (s == ".") break;
		int ans = 0;
		P = Q = R = 0;
		rep(P, 3) rep(Q, 3) rep(R, 3) if (dfs(0, s.length()) == 2) ans++;
		cout << ans << endl;
	}
	return 0;
}
