#include <bits/stdc++.h>
using namespace std;
int p, q, r, now;
string s;
int formula() {
  if (isdigit(s[now]))
    return s[now] - '0';
  else if (s[now] == 'P') {
    now++;
    return p;
  } else if (s[now] == 'Q') {
    now++;
    return q;
  } else if (s[now] == 'R') {
    now++;
    return r;
  }
  int res = 0;
  if (s[now] == '-') {
    now++;
    return 2 - formula();
  } else if (s[now] == '(') {
    now++;
    int res = formula();
    if (s[now] == '*') {
      now++;
      res = min(res, formula());
    } else if (s[now] == '+') {
      now++;
      res = max(res, formula());
    }
    now++;
  }
  return res;
}
int main(void) {
  while (cin >> s, s != ".") {
    int cnt = 0;
    for ((p) = 0; (p) < (3); p++)
      for ((q) = 0; (q) < (3); q++)
        for ((r) = 0; (r) < (3); r++) {
          now = 0;
          if (formula() == 2) cnt++;
        }
    cout << cnt << endl;
  }
  return 0;
}
