#include<iostream>
#include<algorithm>
#include<string>
#include<cctype>
#define rep(i,n) for((i)=0;(i)<(n);i++)

using namespace std;

int PQR[3],now;
string exp;

int fomula(){

  now++;
  if(exp[now-1]=='('){
    int res=fomula();
    now++;
    if(exp[now-1]=='+')res=max(res,fomula());
    if(exp[now-1]=='*')res=min(res,fomula());
    now++;
    return res;
  }
  if(isdigit(exp[now-1]))return exp[now-1]-'0';
  if(isupper(exp[now-1]))return PQR[exp[now-1]-'P'];
  if(exp[now-1]=='-')return 2-fomula();
}

int main(void){

  while(cin >> exp,exp!="."){
    int cnt=0;
    rep(PQR[0],3)rep(PQR[1],3)rep(PQR[2],3){
      now=0;
      cnt+=(2==fomula());
    }
    cout << cnt << endl;
  }
  return 0;
}