#include<iostream>
#include<iomanip>
#include<string>
#include<cstring>
#include<vector>
#include<set>
#include<cmath>
using namespace std;
typedef vector<int> vi;
const int MAX_V=1e3+5;
const int INF=1e9;
vector< vector<int> > board(25,vector<int>(25,0) );
int w,h;
int sy,sx,gy,gx;
int dx[]={-1,0,1,0};
int dy[]={0,-1,0,1};
int goal=100;
//Prototype
int expr(string& s, int& i);
int term(string& s, int& i);
int factor(string& s, int& i);
int number(string& s, int& i);


int number(string &s,int &i)
{
  int n=s[i++]-'0';
  while(isdigit(s[i])) n=n*10+s[i++]-'0';
  return n;
}

int factor(string &s,int &i)
{
  if(isdigit(s[i])) return number(s,i);
  assert(s[i]=='(');
  i++;
  int ret=expr(s,i);
  i++;
  return ret;
}
int term(string &s, int &i)
{
  int val;
  if(s[i]!='-')
    {
      val=factor(s,i);
    }
  while(s[i]=='-')
    {
      i++;
      int val2=term(s,i);
      if(val2==0) val=2;
      if(val2==1) val=1;
      if(val2==2) val=0;
    }
  return val;
}
int expr(string &s, int &i)
{
  int val=term(s,i);
  while(s[i]=='*' || s[i]=='+')
    {
      char op=s[i];
      i++;
      int val2=term(s,i);
      if(op=='*') val=min(val,val2);
      else if(op=='+') val=max(val,val2); 
    }
  return val;
}
int main(void)
{
  string s;
  while(cin >> s && s!=".")
    {
      int ans=0;
      string origin=s;
      for(int r=0;r<=2;r++)
	{
	  for(int p=0;p<=2;p++)
	    {
	      for(int q=0;q<=2;q++)
		{
		  s=origin;
		  for(int i=0;i<int(s.size());i++)
		    {
		      if(s[i]=='P') s[i]='0'+p;
		      if(s[i]=='Q') s[i]='0'+q;
		      if(s[i]=='R') s[i]='0'+r;
		    }
		  //cout << s << endl;
		  int i=0;
		  if(expr(s,i)==2) ans++;
		}
	    }
	}
      cout << ans << endl;
    }
  return 0;
 
}

