#include <iostream>
#include <algorithm>
#include <string>

using namespace std;

int p, q, r;

int formula(string str){
  if(str == "0") return 0;
  if(str == "1") return 1;
  if(str == "2") return 2;
  if(str == "P") return p;
  if(str == "Q") return q;
  if(str == "R") return r;
  if(str[0] == '-') return 2 - formula(str.substr(1));
  int size = str.size();
  int cnt = 0;
  for(int i=0;i<str.size();i++){
    if(str[i] == '(') cnt++;
    if(str[i] == ')') cnt--;
    if(str[i] == '*' && cnt == 1) return min(formula(str.substr(1, i-1)), formula(str.substr(i+1, size-i-2)));
    if(str[i] == '+' && cnt == 1) return max(formula(str.substr(1, i-1)), formula(str.substr(i+1, size-i-2)));
  }
}

int solve(string str){
  int res = 0;
  for(p=0;p<=2;p++){
    for(q=0;q<=2;q++){
      for(r=0;r<=2;r++){
        if(formula(str) == 2) res++;
      }
    }
  }
  return res;
}

int main(){
  string in;
  while(cin >> in){
    if(in == ".") break;
    cout << solve(in) << endl;
  }
}