#include<iostream>
#include<vector>
#include<map>
#include<string>
#include<stack>
#define INF 1000000
using namespace std;

string str;
int table[3];
stack<int> Stack;
int pos;

int opnot(int a){
  return 2 - a;
}

int opand(int a,int b){
  if(a>b)return b;
  else return a;
}

int opor(int a,int b){
  if(a>b)return a;
  else return b;
}

int formula(){
  if(str[pos]=='0' || str[pos]=='1' || str[pos]=='2'){
    return str[pos++]-'0';
  }else if(str[pos]=='P' || str[pos]=='Q' || str[pos]=='R'){
    return table[str[pos++]-'P'];
  }else if(str[pos]=='-'){
    pos++;
    return opnot(formula());
  }else if(str[pos]=='('){
    pos++;
    int ans=formula();
    if(str[pos] == '*'){
      pos++;
      int res = opand(ans,formula());
      pos++;
      return res;
    }else{
      pos++;
      int res = opor(ans,formula());
      pos++;
      return res;
    }
  }
}
int main(){
  int count;
  Stack.push(INF);
  while(cin>>str){
    if(str==".")break;
    count=0;
    for(int i=0;i<3;i++){
      for(int j=0;j<3;j++){
	for(int k=0;k<3;k++){
	  pos=0;
	  table[0]=i;
	  table[1]=j;
	  table[2]=k;
	  if(formula()==2) count++;
	}
      }
    }
    cout<<count<<endl;
  }
  return 0;
}