#include <bits/stdc++.h>
using namespace std;

#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define abs(a) max((a),-(a))
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define repe(i,n) rep(i,(n)+1)
#define per(i,n) for(int i=(int)(n)-1;i>=0;i--)
#define pere(i,n) rep(i,(n)+1)
#define all(x) (x).begin(),(x).end()
#define SP <<" "<<
#define RET return 0
#define MOD 1000000007
#define INF 1000000000000000000

typedef long long LL;
typedef long double LD;

int minu[3]={2,1,0};
int an[3][3]={
  {0,0,0},{0,1,1},{0,1,2}
};
int o[3][3]={
  {0,1,2},{1,1,2},{2,2,2}
};
int p,q,r;

int solve(string s){
  if(s=="0"||s=="1"||s=="2") return stoi(s);
  if(s=="P") return p;
  if(s=="Q") return q;
  if(s=="R") return r;
  if(s[0]=='-'){
    return minu[solve(s.substr(1))];
  }else{
    int count=0;
    for(int i=0;i<s.length();i++){
      if(s[i]=='('){
        count++;
      }else if(s[i]==')'){
        count--;
      }
      if(count==1&&s[i]=='*'){
        return an[solve(s.substr(1,i-1))][solve(s.substr(i+1,s.length()-i-2))];
      }
      if(count==1&&s[i]=='+'){
        return o[solve(s.substr(1,i-1))][solve(s.substr(i+1,s.length()-i-2))];
      }
    }
  }
}

int main(){
  while(1){
    string s;
    cin >> s;
    if(s==".") return 0;
    int ans=0;
    for(int i=0;i<3;i++){
      for(int j=0;j<3;j++){
        for(int k=0;k<3;k++){
          p=i,q=j,r=k;
          if(solve(s)==2) ans++;
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

