#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<cctype>

using namespace std;

int PQR[3],NOT[3]={2,1,0},now;
string exp;

int fomula(){
  if(exp[now]=='('){
    now++;
    int res=fomula();
    if(exp[now]=='+'){
      now++;
      res=max(res,fomula());
    }
    if(exp[now]=='*'){
      now++;
      res=min(res,fomula());
    }
    now++;
    return res;
  }
  else if(isdigit(exp[now])){
    return exp[now++]-'0';
  }
  else if(isupper(exp[now])){
    return PQR[exp[now++]-'P'];
  }
  else if(exp[now]=='-'){
    now++;
    return NOT[fomula()];
  }
}

int main(void){

  while(cin >> exp){
    if(exp==".")break;
    int cnt=0;
    for(PQR[0]=0;PQR[0]<3;PQR[0]++){
      for(PQR[1]=0;PQR[1]<3;PQR[1]++){
	for(PQR[2]=0;PQR[2]<3;PQR[2]++){
	  now=0;
	  cnt+=(2==fomula());
	}
      }
    }
    cout << cnt << endl;
  }
  return 0;
}