#include <iostream>
#include <string>
#define S str[ima]
using namespace std;
string str;
int ima;

int formula(int p,int q,int r) {
  int val;
  int val2;
    while(S=='0'||S=='1'||S=='2'||S=='P'||S=='Q'||S=='R'||S=='('||S=='-') {
    if(S=='('){
      ima++;
      val = formula(p,q,r);

      if(S == '+'){
	ima++;
	val2 = formula(p,q,r);
	if(val==2 || val2==2) val = 2;
	else if(val==1 || val2==1) val = 1;
	else val = 0;
      }
      else if(S == '*') {
	ima++;
	val2 = formula(p,q,r);
	if(val==0 || val2==0) val = 0;
	else if(val==1 || val2==1) val = 1;
	else val = 2;
      }
      ima++;
    }
    
    else if(S=='0'||S=='1'||S=='2'||S=='P'||S=='Q'||S=='R'){
      val = S-'0';
      if(S=='P')val = p;
      else if(S=='Q') val = q;
      else if(S=='R') val = r;
      ima++;
    }
    else if(S == '-'){
      ima++;
      val2 = formula(p,q,r);
      //cout <<"-"<<val2<<endl;
      if(val2 == 0) val = 2;
      else if(val2==2)val = 0;
      else val = 1;
    }
     
    }

  return val;    
    
}
  
int main(){

  while(1) {
    cin >> str;
    if(str == ".") break;
    
    int ans = 0;
    for(int i=0;i<3;i++)
      for(int j=0;j<3;j++)
	for(int k=0;k<3;k++){
	  ima =0;
	  int res = formula(i,j,k);
	  //  cout<<i<<" "<<j<<" "<<k<<" " << res <<endl;
	  //cout << str.size()<<" "<<ima<<endl;
	  if(res == 2) ans++;
	}
    cout << ans <<endl;
  }

  
  return 0;
}