#include <iostream>
#include <vector>
#include <cctype>
#include <string>
#include <cassert>
using namespace std;
string S;
int PQR[3];
int cur = 0;
int formula();
int kakko();
int formula(){
  char c = S[cur];
  cur++;
  if(isdigit(c)){
    return c -'0';
  }else if(isupper(c)){
    return PQR[c-'P'];
  }else if(c == '-'){
    int tmp = formula();
    if(tmp == 2)return 0;
    else if(tmp == 1)return 1;
    else return 2;
  }else{
    int tmp = kakko();
    return tmp;
  }
  return -1;
}
int kakko(){
  int f = formula();
  char op = S[cur];
  cur++;
  int s = formula();
  assert(S[cur] == ')');
  cur++;
  if(op == '*'){
    //cout << f << " " << s << endl;
    return min(f,s);
  }else if(op == '+'){
    return max(f,s);
  }
  return -1;
}
int main(){
  while(cin >> S,S != "."){
    int cnt = 0;
    for(int i = 0;i < 3;i++){
      for(int j = 0;j < 3;j++){
	for(int k = 0;k < 3;k++){
	  cur = 0;
	  PQR[0] = i;
	  PQR[1] = j;
	  PQR[2] = k;
	  if(formula() == 2)cnt++;
	}
      }
    }
      
    
    cout << cnt << endl;
  }
}