#include <stdio.h>

char str[81];
int P, Q, R, ans, p;

int min(int a, int b){ return a > b ? b : a; }
int max(int a, int b){ return a < b ? b : a; }

int formula(){
  int x, res;
  if(str[p] == '('){
    p++;
    x = formula();
    if(str[p++] == '*') res = min(x, formula());
    else res = max(x, formula());
    p++;
  }else if(str[p] == '-'){
    p++;
    res = 2 - formula();
  }else{
    if(str[p] == '0') res = 0;
    if(str[p] == '1') res = 1;
    if(str[p] == '2') res = 2;
    if(str[p] == 'P') res = P;
    if(str[p] == 'Q') res = Q;
    if(str[p] == 'R') res = R;
    p++;
  }
  return res;
}

main(){
  while(1){
    scanf("%s", str);
    if(str[0] == '.') break;
    ans = 0;
    for(P=0;P<3;P++){
      for(Q=0;Q<3;Q++){
        for(R=0;R<3;R++){
          p = 0;
          if(formula() == 2) ans++;
        }
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}