#include <bits/stdc++.h>

using namespace std;

int P, Q, R;
string str;
int pos;

int formula() {
  pos ++;
  int f;
  if(str[pos] == '(') {
    f = formula(); pos ++;
    if(str[pos] == '+') {
      f = max(f, formula());
    }
    if(str[pos] == '*') {
      f = min(f, formula());
    }
    pos ++;
    return f;
  }
  
  if(str[pos] == '-') {
    return 2-formula();
  }
  
  if(isdigit(str[pos])) {
    return str[pos]-'0';
  }
  
  if(str[pos] == 'P') return P;
  if(str[pos] == 'Q') return Q;
  if(str[pos] == 'R') return R;
    
}

int main() {
  while(cin >> str && str != ".") {
    int cnt = 0;
    for(P=0; P<=2; P++)
      for(Q=0; Q<=2; Q++)
	for(R=0; R<=2; R++) {
	  pos = -1;
	  if(formula() == 2) cnt ++;
	}
    
    cout << cnt << endl;
  }
  return 0;
}