#include<iostream>
#include<string>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
string NEG[]={"2","1","0"},AND[][3]={{"0","0","0"},{"0","1","1"},{"0","1","2"}},
OR[][3]={{"0","1","2"},{"1","1","2"},{"2","2","2"}};
string replaceAll(string s,string f,string t) {
	string r;
	for(int p=0;(p=s.find(f))!=s.npos;)r+=s.substr(0,p)+t,s=s.substr(p+f.size());
	return r+s;
}
string replace(string s,string f,string t){
	string r;
	int p=s.find(f);
	if (p!=s.npos)r+=s.substr(0,p)+t,s=s.substr(p+f.size());
	return r+s;
}
int main(){
	string s,t,nt,n,a,o,nm;
	while(cin>>s,s!="."){
		int c=0;
		rep(p,3)rep(q,3)rep(r,3){
			nm=" ";nm[0]='0'+p;
			t=replaceAll(s,"P",nm);nm[0]='0'+q;
			t=replaceAll(t,"Q",nm);nm[0]='0'+r;
			t=replaceAll(t,"R",nm);
			for(nt=t;;t=nt){
				rep(i,3){
					n="- ",n[1]=i+'0';
					nt=replaceAll(nt,n,NEG[i]);
					rep(j,3){
						a="(X*X)",a[1]=i+'0',a[3]=j+'0';
						o="(X+X)",o[1]=i+'0',o[3]=j+'0';
						nt=replaceAll(nt,a,AND[i][j]);
						nt=replaceAll(nt,o,OR[i][j]);
					}
				}
				if(t==nt)break;
			}
			if(t=="2")c++;
		}
		cout<<c<<endl;
	}
	return 0;
}