#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int pqr[3];
int fom(string h);
int main(){
  string h;
  while(getline(cin,h)){
    if(h == "."){break;}
    int ans = 0;
    for(int i=0; i<27; i++){
      pqr[0] = i%3;
      pqr[1] = (i/3)%3;
      pqr[2] = (i/9)%3;
      if(fom(h) == 2){ans++;}
    }
    cout <<ans<<endl;
  }
  return 0;
}
int fom(string h){
  int one,two,dow,an;
  bool f = false;
  one = two = dow = an = -1;
  for(int i=0; i<h.size(); i++){
    if(h[i] == '0'){dow = 0;}
    else if(h[i] == '1'){dow = 1;}
    else if(h[i] == '2'){dow = 2;}
    else if(h[i] == 'P'){dow = pqr[0];}
    else if(h[i] == 'Q'){dow = pqr[1];}
    else if(h[i] == 'R'){dow = pqr[2];}
    else if(h[i] == '-'){f = (f+1)%2;}
    else if(h[i] == '+'){an = 0;}
    else if(h[i] == '*'){an = 1;}
    else if(h[i] == '('){
      int j,dps = 0;
      for(j = 1; j+i<h.size(); j++){
	if(h[j+i] == ')'){
	  if(dps != 0){dps--;}
	  else{break;}
	}
	if(h[j+i] == '('){dps++;}
      }
      dow = fom(h.substr(i+1,j-1));
      i = i+j;
    }
    if(dow != -1){
      if(f){dow = 2-dow;}
      if(one == -1){one = dow;}
      else{two = dow;}
      dow = -1;
      f = 0;
    }
    if(one != -1 && two != -1){
      if(an == 0){one = max(one,two);}
      if(an == 1){one = min(one,two);}
      an = -1;
      two = -1;
    }
  }
  return one;
}