#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
typedef long long i64;

int hitei(int a){
  return (2-a);
}

int whereis(string s){
  int i=0;
  bool kakko = false;
  int continuing = 0;
  while(true){
    if((s[i]=='*' || s[i]=='+') && (!kakko)){
      return i;
    }else if(s[i]=='('){
      kakko=true;
      continuing++;
    }else if(s[i]==')'){
      continuing--;
      kakko=(continuing!=0);
    }
    i++;
  }
}

int calc(string s){
  switch(s[0]){
  case '(':
    {
      string kakkohazushi = s.substr(1,s.length()-2);
      int enzanshi = whereis(kakkohazushi);
      if(kakkohazushi[enzanshi]=='*'){
	return min(calc(kakkohazushi.substr(0,enzanshi)),calc(kakkohazushi.substr(enzanshi+1,kakkohazushi.length()-enzanshi-1)));
      }else{
	return max(calc(kakkohazushi.substr(0,enzanshi)),calc(kakkohazushi.substr(enzanshi+1,kakkohazushi.length()-enzanshi-1)));
      }
    }
    break;
  case '-':
    return hitei(calc(s.substr(1,s.length()-1)));
    break;
  default:
    return (int)(s[0]-'0');
  }
}

int main(){
  while(true){
    string s;
    cin >> s;
    if(s==".") break;
    int count = 0;
    rep(p,3){
      rep(q,3){
        rep(r,3){
	  string a = s;
	  replace(a.begin(),a.end(),'P',to_string(p)[0]);
	  replace(a.begin(),a.end(),'Q',to_string(q)[0]);
	  replace(a.begin(),a.end(),'R',to_string(r)[0]);
	  if(calc(a)==2) count++;
	}
      }
    }
    cout << count << endl;
  }
  return 0;
}