#include<iostream>
#include<map>
#include<algorithm> // min, max
#include<cctype> // isdigit
#include<cassert> //assert
using namespace std;

string exp;
int pos;
map<char,int>value;

int formula()
{
  if(isdigit(exp[pos])){
    return exp[pos++]-'0';
  }
  if(isalpha(exp[pos])){
    return value[ exp[pos++] ];
  }
  if(exp[pos]=='-'){
    pos++;
    return 2-formula();
  }
  if(exp[pos]=='('){
    pos++;
    int left=formula();
    char op=exp[pos++];
    int right=formula();
    assert(exp[pos++]==')');
    if(op=='*'){
      return min(left,right);
    }
    if(op=='+'){
      return max(left,right);
    }
  }
}

int main()
{
  int ans;
  while(cin>>exp,exp!="."){
    ans=0;
    for(int p=0;p<=2;p++)for(int q=0;q<=2;q++)for(int r=0;r<=2;r++){
      value['P']=p;
      value['Q']=q;
      value['R']=r;
      pos=0;
      if(formula()==2)ans++;
    }
    cout<<ans<<endl;
  }
}