#include<cstdio>
#include<algorithm>
#include<vector>
#include<deque>
#include<string>
#include<iostream>
using namespace std;
string mon;
int P,Q,R,it;
int solve(void){//?????°???????????????????????????
	it++;
	if(mon[it]=='0'){return 0;}
	if(mon[it]=='1'){return 1;}
	if(mon[it]=='2'){return 2;}
	if(mon[it]=='P'){return P;}
	if(mon[it]=='Q'){return Q;}
	if(mon[it]=='R'){return R;}
	if(mon[it]=='-'){return 2-solve();}
	if(mon[it]=='('){
		int sk,at;
		char en;
		sk=solve();
		it++;
		en=mon[it];
		at=solve();
		it++;
		if(en=='+'){return max(sk,at);}
		else{return min(sk,at);}
	}
	return 9999;
	cout<<"BUG!!!"<<endl;
}
int main(void){//solve????§£????????????
	int ans;
	while(-1){
		mon.clear();cin>>mon;ans=0;
		if(mon=="."){return 0;}
		for(P=0;P<3;P++){for(Q=0;Q<3;Q++){for(R=0;R<3;R++){
			it=-1;//?????????+1????????????????????§????????????
			if(solve()==2){ans++;}
		}}}
		printf("%d\n",ans);
	}
	return 0;
}