#include<bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define REP(i,n) for(int i=0;i<(int)(n);i++)

using namespace std;
char stck[80];
char NOT(char a){return 98-a;}
char AND(char a,char b){
  return min(a,b);
}
char OR(char a,char b){
  return max(a,b);
}
bool is012(char c){return '0'<=c&&c<='2';}
int main(){ _;
  string s;
  while(cin>>s,s[0]!='.'){
    int sum=0;
    REP(p,3)REP(q,3)REP(r,3){
      int ptr=0;
      for(char c:s){
	stck[ptr]=(c=='P'?p+'0':c=='Q'?q+'0':c=='R'?r+'0':c);
	ptr++;
	while(true){
	  if(ptr>=2&&stck[ptr-2]=='-'&&is012(stck[ptr-1])){
	    stck[ptr-2]=NOT(stck[ptr-1]);
	    ptr--;
	  }else if(ptr>=3&&is012(stck[ptr-3])&&stck[ptr-2]=='*'&&is012(stck[ptr-1])){
	    stck[ptr-3]=AND(stck[ptr-3],stck[ptr-1]);
	    ptr-=2;
	  }else if(ptr>=3&&is012(stck[ptr-3])&&stck[ptr-2]=='+'&&is012(stck[ptr-1])){
	    stck[ptr-3]=OR(stck[ptr-3],stck[ptr-1]);
	    ptr-=2;
	  }else if(ptr>=3&&stck[ptr-3]=='('&&is012(stck[ptr-2])&&stck[ptr-1]==')'){
	    stck[ptr-3]=stck[ptr-2];
	    ptr-=2;
	  }else break;
	}
	//	REP(i,ptr)cout<<stck[i];
	//	cout<<endl;
      }
      if(stck[0]=='2')sum++;
    }
    cout<<sum<<endl;
  }
}