#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <cctype>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()

int seki[3][3] = {{0,0,0},{0,1,1},{0,1,2}};
int wa[3][3] = {{0,1,2},{1,1,2},{2,2,2}};
int hoge[3] = {2,1,0};

int p,q,r;
int eval(string s) {
  //  cout << s << endl;
  if (s[0]=='-') {
    int i;
    for (i=0;s[i]=='-';i++);
    if (i%2) return hoge[eval(s.substr(i))];
    else return eval(s.substr(i));
  }
  if (s[0]!='(') {
    int foo;
    if (isdigit(s[0])) foo = s[0]-'0';
    if (s[0]=='P') foo = p;
    if (s[0]=='Q') foo = q;
    if (s[0]=='R') foo = r;
    return foo;
  }
  int nest = 0;
  REP(i,s.length()) {
    if (s[i] == '(') nest++;
    if (s[i] == ')') nest--;
    if (nest!=1) continue;
    if (s[i] == '+') return wa[eval(s.substr(1,i-1))][eval(s.substr(i+1,s.length()-i-2))];
    if (s[i] == '*') return seki[eval(s.substr(1,i-1))][eval(s.substr(i+1,s.length()-i-2))];
  }
}


int main() {
  string s;
  while(cin>>s,s!=".") {
    int ans = 0;
    for(p=0;p<3;p++) {
      for(q=0;q<3;q++) {
        for(r=0;r<3;r++) {
          if (eval(s)==2)
            ans++;
        }
      }
    }
    cout << ans << endl;
  }
  
}