#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <algorithm>

#define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define sz size()
#define pb push_back
#define mp make_pair
#define ALL(X) (X).begin(),(X).end()

using namespace std;

const int INF = 1000000000;
const double eps = 1e-8;

pair<int,int> val(std::string f) {
  if(f[0] == '-') {
    auto p = val(f.substr(1));
    return mp(2-p.first,p.second+1);
  }else if(f[0] == '(') {
    auto p1 = val(f.substr(1));
    auto p2 = val(f.substr(p1.second+2));
    if(f[p1.second+1] == '*') {
      return mp(min(p1.first,p2.first),p1.second+p2.second+3);
    }else{
      return mp(max(p1.first,p2.first),p1.second+p2.second+3);
    }
  }else{
    int v = f[0] - '0';
    return mp(v,1);
  }
}

int main(void) {
  while(1) {
    string str;
    cin>>str;
    if(str==".")break;
    int count =0;
    REP(i,3)REP(j,3)REP(k,3){
      string cp = str;
      for(char& c : cp){
        switch(c){
          case 'P':
          c=i+'0';
          break;
          case 'Q':
          c=j+'0';
          break;
          case 'R':
          c=k+'0';
          break;
        }
      }
      if(val(cp).first == 2)
        ++count;
    }
    cout<<count<<endl;
  }
  return 0;
}