#include<bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define INF 1000000010
#define EPS 1e-9
#define fst first
#define scd second

#define debug(x) cout<<x<<endl;
#define repi(i,x,n) for(int i=x;i<n;i++)
#define rep(i,n) repi(i,0,n)
#define lp(i,n) repi(i,0,n)
#define repn(i,n) for(int i=n;i>=0;i--)
#define int long long
#define endl "\n"

typedef string::const_iterator State;
class ParseError {};

int num[3];

int formula(State &begin){
  int res,res2;
  if(*begin == '('){
    begin++;
    res=formula(begin);
    if(*begin=='*'){
      begin++;
      res2=formula(begin);
       begin++;
      return min(res,res2);
    }else if(*begin=='+'){
      begin++;
      res2=formula(begin);
       begin++;
      return max(res,res2);
    }
   
  }
  if(*begin=='-'){
    begin++;
    return 2-formula(begin);
  }
  if(isdigit(*begin)){
    res=(*begin)-'0';
    begin++;
    return res;
  }
  if('P'<=(*begin) && (*begin)<='R'){
    res=num[(*begin)-'P'];
    begin++;
    return res;
  }
  
}




signed main(){
  cin.tie(0);	
  //ios::sync_with_stdio(false);
  while(1){
    string s;
    cin>>s;
    if(s==".") break;
    int ans=0;
    rep(i,3){
      num[0]=i;
      rep(j,3){
	num[1]=j;
	rep(k,3){
	  num[2]=k;
	  State begin=s.begin();
	  if(formula(begin)==2){
	    // cout<<num[0]<<" "<<num[1]<<" "<<num[2]<<endl;
	    ans++;
	  }
	}
      }
    }
    cout<<ans<<endl;
  }
    

  return 0;
}

