#include <iostream>
#include <string>
#include <algorithm>
#define S str[ima]
using namespace std;
string str;
int ima;

int formula(int P,int Q,int R) {
  int res;
  while(S=='0' || S=='1' || S=='2' || S=='P'||S=='Q'||S=='R'||S=='-'||S=='('){
    if(S=='0' || S=='1' || S=='2' || S=='P'||S=='Q'||S=='R'){
      res = S-'0';
      if(S=='P') res = P;
      if(S=='Q') res = Q;
      if(S=='R') res = R;
      ima++;
    }
    else if(S=='-') {
      ima++;
      res = formula(P,Q,R);
      if(res == 0) res = 2;
      else if(res == 2) res = 0;
    }
    else if(S=='(') {
      ima++;
      res = formula(P,Q,R);
      if(S=='*')ima++, res = min(formula(P,Q,R),res);
      if(S=='+')ima++, res = max(formula(P,Q,R),res);
      ima++;
    }
  }
  return res;
}

int main() {

  while(1) {
    cin >> str;
    if(str==".") break;
    int ans = 0;
    for(int i=0;i<3;i++)
      for(int j=0;j<3;j++)
	for(int k=0;k<3;k++){
	  ima = 0;
	  if(formula(i,j,k) == 2) ans++;
	}
    cout <<ans <<endl;
  }
  return 0;
}