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

string S;

int and3(int s1, int s2) {
  if(s1 == 2 && s2 == 2) return 2;
  if(s1 == 0 || s2 == 0) return 0;
  return 1;
}

int or3(int s1, int s2) {
  if(s1 == 0 && s2 == 0) return 0;
  if(s1 == 2 || s2 == 2) return 2;
  return 1;
}

int calc(int p, int q, int r) {
  stack<char> stk;

  REP(i, 0, S.length()) {
    if(S[i] == '(' || S[i] == '-' || S[i] == '*' || S[i] == '+') stk.push(S[i]);
    if(('0' <= S[i] && S[i] <= '2') || ('P' <= S[i] && S[i] <= 'R')) {
      int d;
      if('0' <= S[i] && S[i] <= '2') d = S[i] - '0';
      if(S[i] == 'P') d = p;
      if(S[i] == 'Q') d = q;
      if(S[i] == 'R') d = r;
      while(!stk.empty() && stk.top() == '-') {
        stk.pop();
        d = 2 - d;
      }
      stk.push(d);
    }
    if(S[i] == ')') {
      int s2 = stk.top(); stk.pop();
      int op = stk.top(); stk.pop();
      int s1 = stk.top(); stk.pop();
      stk.pop();
      int d;
      if(op == '*') d = and3(s1, s2);
      if(op == '+') d = or3(s1, s2);
      while(!stk.empty() && stk.top() == '-') {
        stk.pop();
        d = 2 - d;
      }
      stk.push(d);
    }
  }

  return stk.top();
}

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

  return 0;
}