#include <iostream>
#include <string>


using namespace std;


int t;
string s;

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

int d[3];

int form(){

	if (s[t] >= '0' && s[t] <= '2'){
		int p = s[t] - '0';
		t++;
		return p;
	}

	if (s[t] >= 'P' && s[t] <= 'R'){
		int p = s[t] - 'P';
		t++;
		return d[p];
	}

	if (s[t] == '-'){
		t++;
		int q = form();
		return (q == 0 ? (2) : ((q == 2) ? 0 : 1));
	}

	if (s[t] == '('){
		t++;
		int p = form();
		if (s[t] == '*'){
			t++; int q = form(); t++; return tbk[p][q];
		}
		else
			if (s[t] == '+'){
				t++; int q = form(); t++; return tbp[p][q];
			}
	}
}

int ans = 0;

void dfs(int i){

	if (i == 3){
		t = 0;
		if (form() == 2)ans++;
		return;
	}

	for (int j = 0; j < 3; j++){
		d[i] = j;
		dfs(i + 1);
	}
}


int main()
{
	while (cin >> s && s != "."){
		t = 0;
		ans = 0;
		s += " ";
		dfs(0);
		cout << ans << endl;
	}

	return 0;
}