#include <iostream>

using namespace std;

char* s;
int p,q,r;

int formula()
{
	if(isdigit(*s) || isupper(*s)){
		char c=*s++;
		if(isdigit(c)) return c-'0';
		if(c=='P') return p;
		if(c=='Q') return q;
		if(c=='R') return r;
	}
	if(*s=='-'){
		s++;
		int v=formula();
		return 2-v;
	}
	if(*s=='('){
		s++;
		int a=formula();
		char op=*s++;
		int b=formula();
		s++;
		if(op=='*') return min(a,b);
		if(op=='+') return max(a,b);
	}
	return -1;
}

int main()
{
	for(char s[81];cin>>s,s[0]!='.';){
		int res=0;
		for(p=0;p<3;p++)
			for(q=0;q<3;q++)
				for(r=0;r<3;r++){
					::s=s;
					if(formula()==2)
						res++;
				}
		cout<<res<<endl;
	}
	return 0;
}