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

map<char,int> val;
map<char,function<int(int,int)>> op;

bool nextval(){
  val['R']++;
  if(val['R'] == 3){
    val['R'] = 0;
    val['Q']++;
    if(val['Q'] == 3){
      val['Q'] = 0;
      val['P']++;
      if(val['P'] == 3){
	val['P'] = 0;
	return false;
      }
    }
  }
  return true;
}

int solve(const string &s,int &i){
  if(isdigit(s[i])){
    return s[i++] - '0';
    
  }else if(val.count(s[i])){
    return val[s[i++]];
    
  }else if(s[i] == '-'){
    i++; // '-'
    return  2 - solve(s,i);

  }else if(s[i] == '('){
    i++; // '('
    int l = solve(s,i);
    char m = s[i];
    assert(op.count(m));
    i++; // '+' or '*'
    int r = solve(s,i);
    i++; // ')'
    return op[m](l,r);
  }
  assert(false);
}

int main(void){
  string s;
  val['P'] = val['Q'] = val['R'] = 0;
  op['+'] = [](int x,int y){
    if(x == 2 or y == 2) return 2;
    else if(x == 1 or y == 1) return 1;
    else return 0;
  };
  op['*'] = [](int x,int y){
    if(x == 0 or y == 0) return 0;
    else if(x == 1 or y == 1) return 1;
    else return 2;
  };
  
  while(cin >> s, s != "."){
    int res = 0;
    
    do{
      int index = 0;
      if(solve(s,index) == 2)
	res++;
    }while(nextval()); // 0,0,0で始まり0,0,0で終わる
    
    cout << res << endl;
  }
}
