#include <bits/stdc++.h>
using namespace std;
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) {
  if (s[0] != '(') {
    int i;
    for (i = 0; s[i] == '-'; i++)
      ;
    int foo;
    if (isdigit(s[i])) foo = s[i] - '0';
    if (s[i] == 'P') foo = p;
    if (s[i] == 'Q') foo = q;
    if (s[i] == 'R') foo = r;
    if (i % 2)
      return hoge[foo];
    else
      return foo;
  }
  int nest = 0;
  for (int i = 0; i < (int)s.length(); ++i) {
    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))];
    if (s[i] == '*')
      return seki[eval(s.substr(1, i - 1))][eval(s.substr(i + 1))];
  }
}
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;
  }
}
