#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

string str;

int evaluate(int idx){
  if(str[idx]=='('){
    int depth=1;
    int idx2=idx+1;
    while(depth>0){
      if(str[idx2]=='('){
        depth++;
      }else if(str[idx2]==')'){
        depth--;
      }else if(str[idx2]=='+' && depth==1){
        return max(evaluate(idx+1), evaluate(idx2+1));
      }else if(str[idx2]=='*' && depth==1){
        return min(evaluate(idx+1), evaluate(idx2+1));
      }
      idx2++;
    }
  }else{
    if(str[idx]=='-'){
      return 2-evaluate(idx+1);
    }else{
      return str[idx]-'0';
    }
  }
  return 0;
}

int main(){
  while(1){
    string tmp;
    cin >> tmp;
    if(tmp[0] == '.') break;

    str = tmp;
    int count = 0;
    for(int i=0; i<=2; i++){
      for(int j=0; j<=2; j++){ 
        for(int k=0; k<=2; k++){
          //substitute
          for(int idx=0; idx<(int)tmp.length(); idx++){
            if(tmp[idx]=='P') str[idx]='0'+i;
            else if(tmp[idx]=='Q') str[idx]='0'+j;
            else if(tmp[idx]=='R') str[idx]='0'+k;
            else str[idx]=tmp[idx];
          }
          if(evaluate(0) == 2) count++;
        }
      }
    }
    cout << count << endl;
  }
  return 0;
}