#include <iostream>
#include <stack>
#define REP(i, a, n) for(int i = (a); i < (n); i++)
using namespace std;

string S;

int calc(int p, int q, int r) {
  stack<int> s;
  REP(i, 0, S.length()) {
    int n = -1;
    char c = S[i];
    if(c == '(') s.push('(');
    if(c == '*') s.push('*');
    if(c == '+') s.push('+');
    if(c == 'P') n = p;
    if(c == 'Q') n = q;
    if(c == 'R') n = r;
    if('0' <= c && c <= '2') n = c - '0';
    if(c == '-') s.push('-');
    if(c == ')') {
      int v1 = s.top(); s.pop();
      int op = s.top(); s.pop();
      int v2 = s.top(); s.pop();
      s.pop();
      if(op == '*') {
        if(v1 == 2 && v2 == 2) n = 2;
        else if(v1 == 0 || v2 == 0) n = 0;
        else n = 1;
      }
      if(op == '+') {
        if(v1 == 0 && v2 == 0) n = 0;
        else if(v1 == 2 || v2 == 2) n = 2;
        else n = 1;
      }
    }
    if(n != -1) {
      while(!s.empty() && s.top() == '-') {
        s.pop();
        n = 2 - n;
      }
      s.push(n);
    }
  }

  return s.top();
}

int main(void) {
  while(cin >> S, S != ".") {
    int ans = 0;
    REP(p, 0, 3) REP(q, 0, 3) REP(r, 0, 3) {
      if(calc(p, q, r) == 2) ans++;
    }
    cout << ans << endl;
  }

  return 0;
}