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

#define for_(i,a,b) for(int i=(a);i<(b);++i)

int bar[3] = { 2, 1, 0 };

int mul[3][3] = {
	{ 0, 0, 0 },
	{ 0, 1, 1 },
	{ 0, 1, 2 }
};

int add[3][3] = {
	{ 0, 1, 2 },
	{ 1, 1, 2 },
	{ 2, 2, 2 }
};

string str;
int var[3];

int expr(int&);
int fomula(int&);

int expr(int& i) {
	int val = fomula(i);
	
	while (str[i] == '*' || str[i] == '+') {
		char op = str[i];
		++i;
		int f = fomula(i);
		if (op == '*') val = mul[val][f];
		else val = add[val][f];
	}
	
	return val;
}

int fomula(int& i) {
	if (str[i] == '-') {
		++i;
		return bar[fomula(i)];
	}
	
	if (isdigit(str[i])) { return str[i++] - '0'; }
	if (str[i] == 'P') { ++i; return var[0]; }
	if (str[i] == 'Q') { ++i; return var[1]; }
	if (str[i] == 'R') { ++i; return var[2]; }
	
	++i;
	int res = expr(i);
	++i;
	
	return res;
}

int main() {
	while (cin >> str) {
		if (str == ".") break;
		
		int ans = 0;
		
		for_(p,0,3) for_(q,0,3) for_(r,0,3) {
			var[0] = p; var[1] = q; var[2] = r;
			int i = 0;
			if (expr(i) == 2) ++ans;
		}
		
		cout << ans << endl;
	}
}