#include <bits/stdc++.h>
using namespace std;
int solve(string s, int p, int q, int r) {
  s = regex_replace(s, regex("-0"), "2");
  s = regex_replace(s, regex("-1"), "1");
  s = regex_replace(s, regex("-2"), "0");
  s = regex_replace(s, regex("-P"), to_string(2 - p));
  s = regex_replace(s, regex("-Q"), to_string(2 - q));
  s = regex_replace(s, regex("-R"), to_string(2 - r));
  s = regex_replace(s, regex("P"), to_string(p));
  s = regex_replace(s, regex("Q"), to_string(q));
  s = regex_replace(s, regex("R"), to_string(r));
  string a = "";
  for (auto c : s) {
    a += c;
    if (c == ')') {
      auto p = a.substr(a.size() - 5);
      if (p[2] == '+') {
        a = a.substr(0, a.size() - 5) + max(p[1], p[3]);
      } else {
        a = a.substr(0, a.size() - 5) + min(p[1], p[3]);
      }
    }
  }
  return a == "2" ? 1 : 0;
}
int main(void) {
  string s;
  while (cin >> s, s != ".") {
    auto cp = regex_replace(s, regex("--"), "");
    int ans = 0;
    for (int p = 0; p < 3; p++)
      for (int q = 0; q < 3; q++)
        for (int r = 0; r < 3; r++) {
          ans += solve(cp, p, q, r);
        }
    cout << ans << endl;
  }
}
