#include <iostream>
#include <string>
#include <sstream>

using namespace std;
int p,q,r;
char str[100];
char *sp;

int formula(){
	char c;
	c=*sp++;
	if('0'<=c&&c<'9') return c-'0';
	if(c=='P') return p;
	if(c=='Q') return q;
	if(c=='R') return r;
	if(c=='-') return -(formula()-1)+1;
	if(c=='('){
		int left, right;
		char ope;
		left = formula();
		ope = *sp++;
		right = formula();
		sp++;
		if(ope=='*') return min(left, right);
		if(ope=='+') return max(left, right);
	}
	return 0;
}

int main(){
	while(cin>>str, *str!='.'){
		int sum=0;
		for(p=0;p<3;p++)
		for(q=0;q<3;q++)
		for(r=0;r<3;r++){
			sp = str;
			sum += formula()==2;
		}
		cout<<sum<<endl;
	}
}