#include <bits/stdc++.h>
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;
  for (int i = (0); i < (S.length()); i++) {
    if (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();
      int d;
      if (op == '*') d = and3(s1, s2);
      if (op == '+') d = or3(s1, s2);
      stk.push(d);
    }
  }
  return stk.top();
}
int main(void) {
  while (cin >> S, S != ".") {
    int cnt = 0;
    for (int p = (0); p < (3); p++)
      for (int q = (0); q < (3); q++)
        for (int r = (0); r < (3); r++)
          if (calc(p, q, r) == 2) cnt++;
    cout << cnt << endl;
  }
  return 0;
}
