#include <bits/stdc++.h>
using namespace std;
const int INF = 1001001001;
int formula(const string& str, int& cur) {
  if (isdigit(str[cur])) {
    ++cur;
    return str[cur - 1] - '0';
  }
  if (str[cur] == '-') {
    ++cur;
    return 2 - formula(str, cur);
  }
  ++cur;
  int a = formula(str, cur);
  char op = str[cur];
  ++cur;
  int b = formula(str, cur);
  ++cur;
  if (op == '+')
    return max(a, b);
  else
    return min(a, b);
}
int main() {
  char str[128];
  while (scanf("%s", str), str[0] != '.') {
    int n = strlen(str);
    int cnt = 0;
    for (int p = 0; p < int(3); ++p)
      for (int q = 0; q < int(3); ++q)
        for (int r = 0; r < int(3); ++r) {
          string tmp = str;
          for (int i = 0; i < int(n); ++i) {
            if (tmp[i] == 'P') tmp[i] = p + '0';
            if (tmp[i] == 'Q') tmp[i] = q + '0';
            if (tmp[i] == 'R') tmp[i] = r + '0';
          }
          int cur = 0;
          cnt += formula(tmp, cur) == 2;
        }
    printf("%d\n", cnt);
  }
  return 0;
}
