#include <bits/stdc++.h>
using namespace std;
int main(void) {
  while (1) {
    string s;
    cin >> s;
    if (s == ".") return 0;
    int ans = 0;
    for (int i = 0; i < 27; i++) {
      int p = i % 3, q = (i / 3) % 3, r = (i / 9) % 3;
      int neg = 0, idx = 0;
      vector<char> ope;
      vector<int> val[30];
      for (int j = 0; j < s.size(); j++) {
        if (char(s[j]) == '-')
          neg++;
        else if (char(s[j]) == '+')
          ope.push_back(1);
        else if (char(s[j]) == '*')
          ope.push_back(2);
        else if (char(s[j]) == 'P') {
          if (neg % 2 == 0)
            val[idx].push_back(p);
          else
            val[idx].push_back(2 - p);
          neg = 0;
        } else if (char(s[j]) == 'Q') {
          if (neg % 2 == 0)
            val[idx].push_back(q);
          else
            val[idx].push_back(2 - q);
          neg = 0;
        } else if (char(s[j]) == 'R') {
          if (neg % 2 == 0)
            val[idx].push_back(r);
          else
            val[idx].push_back(2 - r);
          neg = 0;
        } else if (char(s[j]) == '(')
          idx++;
        else if (char(s[j]) == ')') {
          idx--;
          if (ope[ope.size() - 1] == 1) {
            val[idx].push_back(max(val[idx + 1][0], val[idx + 1][1]));
          } else {
            val[idx].push_back(min(val[idx + 1][0], val[idx + 1][1]));
          }
          ope.pop_back();
          val[idx + 1].clear();
        } else {
          if (neg % 2 == 0)
            val[idx].push_back(int(s[j] - '0'));
          else
            val[idx].push_back(2 - int(s[j] - '0'));
          neg = 0;
        }
      }
      if (val[0][0] == 2) {
        ans++;
      }
      for (int j = 0; j < 20; j++) val[j].clear();
      ope.clear();
    }
    cout << ans << endl;
  }
}
