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

string replace(string S, char a, char b){
  string ret = S;
  for(int i = 0; i < S.length(); ++i){
    if(S[i] == a) ret[i] = b;
  }
  return ret;
}

class Parser{
public:
  string s;
  size_t i;
  int parse(){
    i = 0;
    return formula();
  }
  int formula(){
    if(i >= s.length()) return 0;
    if(s[i] >= '0' && s[i] <= '2'){
      ++i;
      return s[i-1] - '0';
    }
    if(s[i] == '-'){
      ++i;
      return 2 - formula();
    }
    if(s[i] == '('){
      ++i;
      int x = formula();
      char o = s[i];
      ++i;
      int y = formula();
      ++i;
      if(o == '*') return min(x,y);
      if(o == '+') return max(x,y);
    }
    return -1;
  }
};


int solve(string S){
  int ret = 0;
  for(int i = 0; i < 3; ++i){
    for(int j = 0; j < 3; ++j){
      for(int k = 0; k < 3; ++k){
        string S_ = S;
        S_ = replace(S_,'P','0'+i);
        S_ = replace(S_,'Q','0'+j);
        S_ = replace(S_,'R','0'+k);
        Parser P;
        P.s = S_;
        if(P.parse() == 2) ++ret;
      }
    }
  }
  return ret;
}

int main(){
  string S;
  while(cin >> S, S != "."){
    cout << solve(S) << endl;
  }
  return 0;
}

