#include <bits/stdc++.h>
using namespace std;

string R(string s,string t,string u){
	int p=s.find(t);
	while(p!=string::npos){
		s.replace(p,t.size(),u);
		p=s.find(t,p+u.size());
	}
	return s;
}
string from[21]={"-0","-1","-2","(0*0)","(0*1)","(0*2)","(1*0)","(1*1)","(1*2)","(2*0)","(2*1)","(2*2)","(0+0)","(0+1)","(0+2)","(1+0)","(1+1)","(1+2)","(2+0)","(2+1)","(2+2)"};
string to[21]={"2","1","0","0","0","0","0","1","1","0","1","2","0","1","2","1","1","2","2","2","2"};
string kaga3(string s){
	while(1<s.size()){
		for(int i=0;i<21;i++)s=R(s,from[i],to[i]);
	}
	return s;
}

int main(){
	
	for(string S,s;cin>>S,S!=".";){
		int ans=0;
		for(char P='0';P<'3';P++){
			for(char Q='0';Q<'3';Q++){
				for(char R='0';R<'3';R++){
					s=S;
					replace(s.begin(),s.end(),'P',P);
					replace(s.begin(),s.end(),'Q',Q);
					replace(s.begin(),s.end(),'R',R);
					if(kaga3(s)=="2")ans++;
				}
			}
		}
		cout<<ans<<endl;
	}
	
	return 0;
}
