#include <iostream>
#include <string>
#include <algorithm>
#define N 3
using namespace std;
int formula();
int p,P,Q,R;
string str;
int main(){
  int r,cnt;
  while(1){
    cin >> str;
    if(str==".") break;
    cnt=0;
    for(int i=0;i<N;i++){
      for(int j=0;j<N;j++){
	for(int k=0;k<N;k++){
	  p=0,P=i,Q=j,R=k;
	  r=formula();
	  if(r==2) cnt++;
	}
      }
    }
    cout << cnt << endl;
  }
  return 0;
}
int formula(){
  int val,val1;
  while(1){
    if(str[p]=='0'||str[p]=='1'||str[p]=='2'){
      p++;
      return str[p-1]-'0';
    }
    else if(str[p]=='P'){
      p++;
      return P;
    }
    else if(str[p]=='Q'){
      p++;
      return Q;
    }
    else if(str[p]=='R'){
      p++;
      return R;
    }
    else if(str[p]=='-'){
      p++;
      val=formula();
      return 2-val;
    }
    else if(str[p]=='('){
      p++;
      val=formula();
    }
    else if(str[p]=='*'){
      p++;
      val1=formula();
      return min(val,val1);
    }
    else if(str[p]=='+'){
      p++;
      val1=formula();
      return max(val,val1);
    }
    else if(str[p]==')') p++;
  }
  return val;
}