#include <bits/stdc++.h>
using namespace std;
map<char, int> nanm;
int nanminus;
int nani;
int narec(string s) {
  if (s[nani] == 'P' || s[nani] == 'Q' || s[nani] == 'R') {
    nani++;
    int minus = nanminus;
    nanminus = 0;
    return pow(-1, minus) * nanm[s[nani - 1]];
  }
  if (isdigit(s[nani])) {
    nani++;
    int minus = nanminus;
    nanminus = 0;
    return pow(-1, minus) * (s[nani - 1] - '0' - 1);
  }
  if (s[nani] == '-') {
    nanminus++;
    nani++;
    return narec(s);
  }
  if (s[nani] == '(') {
    nani++;
    int left = narec(s);
    char op = s[nani];
    nani++;
    int right = narec(s);
    nani++;
    if (op == '*') return min(left, right);
    if (op == '+') return max(left, right);
  }
  return -2;
}
int main() {
  string s;
  int P, Q, R;
  while (cin >> s && s != ".") {
    int ans = 0;
    for (P = -1; P <= 1; P++) {
      for (Q = -1; Q <= 1; Q++) {
        for (R = -1; R <= 1; R++) {
          nanminus = 0;
          nani = 0;
          nanm['P'] = P;
          nanm['Q'] = Q;
          nanm['R'] = R;
          if (narec(s) == 1) {
            ans++;
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
