#include <bits/stdc++.h>
typedef long long LL;
#define SORT(c) sort((c).begin(),(c).end())

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

using namespace std;
map<char,int> logic;
string s;
int ind;
int atom()
{
  int tmp=logic[s[ind]];
  ++ind;
  return tmp;
}
int formula()
{
  if(s[ind]=='-'){
    ++ind;
    return 2-formula();
  }else if(s[ind]=='('){
    ++ind;
    int tmpl=formula();
    int tmp;
    if(s[ind]=='+') tmp=0;
    if(s[ind]=='*') tmp=1;
    ++ind;
    int tmpr=formula();
    ++ind;
    return tmp ? min(tmpl,tmpr) : max(tmpl,tmpr);
  }else return atom();
}

int main(void)
{
  int n;
  REP(i,3)logic['0'+i]=i;
  while(getline(cin,s)){
    if(s==".") break;
    int ans=0;
    REP(p,3) REP(q,3) REP(r,3){
      logic['P']=p;
      logic['Q']=q;
      logic['R']=r;
      ind=0;
      if(formula()==2) ++ans;
    }
    cout << ans << endl;
  }
  return 0;
}