#include<iostream>
#include<algorithm>
#include<string>
#include<cctype>

#define rep(i,n) for((i)=0;(i)<(n);i++)

using namespace std;

int p,q,r,now;
string s;

int formula(void){
	
	if(isdigit(s[now])){return s[now++]-'0';}
	if(s[now]=='P'){now++;return p;}
	if(s[now]=='Q'){now++;return q;}
	if(s[now]=='R'){now++;return r;}
	
	int res=0;
	if(s[now]=='-'){now++;return 2-formula();}
	if(s[now]=='('){
		now++;
		res=formula();
		if(s[now]=='*'){now++;res=min(res,formula());}
		else if(s[now]=='+'){now++;res=max(res,formula());}
		now++;
	}
	return res;
}

int main(void){
	
	while(cin >> s,s!="."){
		
		int cnt=0;
		rep(p,3)rep(q,3)rep(r,3){
			now=0;
			if(formula()==2)cnt++;
		}
		cout << cnt << endl;
	}
	return 0;
}