#include<iostream>
#include<algorithm>
using namespace std;



int p,q,r;
int it;
string s;
int formula()
{
  // cout<<"formula :"<<s[it]<<endl;
  if(s[it]=='-')
    {
      it++;
      int tmp = formula();
      return (-tmp)+2;
    }
  else if(s[it]=='(')
    {
      it++;
      int a = formula();
      it++;
      if(s[it]=='*')
	{
	  it++;
	  int b = formula();
	  it++;
	  //	  cout<<a<<" "<<b<<endl;
	  return min(a,b);
	}
      else
	{
	  it++;
	  int b = formula();
	  it++;
	  return max(a,b);
	}
    }
  else
    {
      char tmp=s[it];
      // it++;
      if(tmp=='0')
	return 0;
      if(tmp=='1')
	return 1;
      if(tmp=='2')
	return 2;
      if(tmp=='P')
	return p;
      if(tmp=='Q')
	return q;
      if(tmp=='R')
	return r;

    }
}

int parse(string s)
{
  //  string::iterator it=s.begin();
  it=0;
  return formula();
}



int main()
{
  for(;cin>>s,s!=".";)
    {
      int cnt=0;
      for(p=0;p<3;p++)
	for(q=0;q<3;q++)
	  for(r=0;r<3;r++)
	    {
	         int ans=parse(s);
	      if(ans==2)
		cnt++;
	      //cout<<p<<" "<<q<<" "<<r<<" "<<ans<<endl;
	    }
      cout<<cnt<<endl;


    }

}