#include <iostream>
#include <cstdlib>
using namespace std;

int P, Q, R;
const char *p;

int formula() {
  
  p++;

  switch( *p ) {
  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;
    p++;
    switch( *p ) {
    case '*':
      form2 = formula();
      p ++;
      if( form1 == 0 || form2 == 0 ) return 0;
      if( form1 == 1 || form2 == 1 ) return 1;
      else return 2;
    case '+':
      form2 = formula();
      p++;
      if( form1 == 2 || form2 == 2 ) return 2;
      if( form1 == 1 || form2 == 1 ) return 1;
      else return 0;
    }
  }
  
  return 0;
}

int main() {
  string str;
  
  while(getline(cin, str)) {
    int cnt = 0;
    if( str == "." ) break;
    for(int i=0; i<3; i++) {
      for(int j=0; j<3; j++) {
	for(int k=0; k<3; k++) {
	  P = i, Q = j, R = k;
	  p = &str[0]-1;
	  if( formula() == 2 ) cnt ++;
	}
      }
    }

    cout << cnt << endl;
  }
  
  return 0;
}