#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
string::iterator it;
int P,Q,R;

int formula(){
  ++it;
  switch(*it){
  case '-':
    return 2-formula();
  case 'P':
    return P;
  case 'Q':
    return Q;
  case 'R':
    return R;
  case '0':
    return 0;
  case '1':
    return 1;
  case '2':
    return 2;

  case '(':
    int form1 = formula();
    int form2;

    ++it;
    switch(*it){
    case '*':
      form2 = formula();
      ++it;
      return min(form1,form2);
    case '+':
      form2 = formula();
      ++it;
      return max(form1,form2);
    }
  }

  return 0;
}

int main(){
  string str;

  while(cin >> str,str != "."){
    int cnt = 0;

    for(int i = 0 ; i < 3 ; i++){
      for(int j = 0 ; j < 3 ; j++){
	for(int k = 0 ; k < 3 ; k++){
	  it = str.begin()-1;
	  P = i, Q = j , R = k;
	  if(formula() == 2) cnt++;
	}
      }
    }
    cout << cnt << endl;
  }

  return 0;
}