#include <stdio.h>
#include <ctype.h>

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

int formula(){
  int l,r;
  char c = *p++;
  if(isdigit(c)) return c-'0';
  if(isupper(c)) return (c == 'P' ? P : c == 'Q' ? Q : R);
  if(c == '-') return 2-formula();
  if(c == '('){
    l = formula(); c = *p++; r = formula(); *p++;
    if(c == '*') return (l < r ? l : r);
    return (l > r ? l : r);
  }
  return 0;
}

int main(){
  char s[100];
  while(scanf("%s",s), s[0] != '.'){
    a = 0; 
    for(P = 0 ; P < 3 ; P++){
      for(Q = 0 ; Q < 3 ; Q++){
        for(R = 0 ; R < 3 ; R++){
          p = s;
          if(formula() == 2) a++;
        }
      }
    }
    printf("%d\n",a);
  }
  return 0;
}