#include <bits/stdc++.h>
using namespace std;
int pqr, now;
string s;
int formula(void) {
  if (isdigit(s[now])) {
    return s[now++] - '0';
  }
  if (isupper(s[now])) {
    now++;
    return (pqr >> (s[now - 1] - 'P')) & 1;
  }
  int res = 0;
  if (s[now] == '-') {
    now++;
    return 2 - formula();
  }
  if (s[now] == '(') {
    now++;
    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 (pqr = 0; pqr < (1 << 3); pqr++) {
      now = 0;
      if (formula() == 2) cnt++;
    }
    cout << cnt << endl;
  }
  return 0;
}
