#include<iostream>
#include<string>
#include<cassert>
using namespace std;
string S;
int cur;
int pqr[3];
int deny[3]={2,1,0};
int multi[3][3]={{0,0,0},{0,1,1},{0,1,2}};
int sum[3][3]={{0,1,2},{1,1,2},{2,2,2}};
int formula();
int Deny(int );
int main(){
  while(cin>>S&&S!="."){
    int cnt=0;
    for(int p=0;p<=2;p++){
      for(int q=0;q<=2;q++){
        for(int r=0;r<=2;r++){
          cur=0;
          pqr[0]=p;pqr[1]=q;pqr[2]=r;
          if(formula()==2)cnt++;
        }
      }
    }
    cout<<cnt<<endl;
  }
}
int formula(){
  if('0'<=S[cur]&&S[cur]<='2'){
    return (int)(S[cur++]-'0');
  }else if('P'<=S[cur]&&S[cur]<='R'){
    return pqr[(int)(S[cur++]-'P')];
  }else if(S[cur]=='-'){
    cur++;
    return Deny(formula());
  }else{
    assert(S[cur++]=='(');
    int left=formula();
    char op=S[cur++];
    // cout<<"op=="<<op<<endl;
    assert(op=='+'||op=='*');
    
    int right=formula();
    assert(S[cur]==')');
    cur++;
    return (op=='+')?sum[left][right]:multi[left][right];
  }
}
int Deny(int a){
  assert(0<=a&&a<=2);
  return deny[a];
}