#include<iostream>
#include<vector>
#include<string>
#define pb  push_back 
#define rep(i,n) for(int i=0; i<(n);i++)
using namespace std;

int mapper[256] = {};

int formula(const string& s, int& i) {
  char c = s[i++];
  int v,v2;
  switch(c) {
  case '0':
  case '1':
  case '2': return c -'0';
  case 'P':
  case 'Q':
  case 'R': return mapper[c];
  case '-':
    v = formula(s,i);
    return (-v+2)%3;
  case '(':
    v = formula(s,i);
    char op = s[i++];
    v2 = formula(s,i);
    i++;
    if (op=='*') {
      return min(v, v2);
    } else {
      return max(v, v2);
    }
  }
  return 0;
}

int main(){
  string s;
  while(cin>>s,s!=".") {

    int count = 0;
    rep(p,3) rep(q,3) rep(r,3) {
      int i = 0;
      mapper['P'] = p;
      mapper['Q'] = q;
      mapper['R'] = r;
      if (formula(s,i) == 2) {
	//cout << "ok " << p << q << r << endl;
	count++;
      }
    }
    cout << count << endl;
  }
}