#include <cstdio>
#include <cassert>
#include <cctype>
#include <vector>
#include <algorithm>
#include <string>

int apply(int lhs, char op, int rhs) {
  if (op == '*') return std::min(lhs, rhs);
  if (op == '+') return std::max(lhs, rhs);
  assert(false);
}

int parse(
    const std::string &s, size_t &i, const std::vector<int> &vars, 
    const std::vector<std::string> &ops={"+*"}, size_t preced=0) {

  if (preced == ops.size()) {
    if (s[i] == '(') {
      int res=parse(s, ++i, vars, ops, 0);
      assert(s[i] == ')');
      ++i;
      return res;
    }
    if (s[i] == '-') {
      ++i;
      return 2-parse(s, i, vars, ops, preced);
    }
    if (isalpha(s[i])) {
      return vars[s[i++]-'P'];
    }
    if (isdigit(s[i])) {
      return s[i++]-'0';
    }
  }
  int res=parse(s, i, vars, ops, preced+1);
  while (i < s.length()) {
    char op=s[i];
    if (!std::count(ops[preced].begin(), ops[preced].end(), op)) break;
    int tmp=parse(s, ++i, vars, ops, preced+1);
    res = apply(res, op, tmp);
  }
  return res;
}

int testcase_ends() {
  char buf[128];
  scanf("%s", buf);
  std::string s=buf;
  if (s == ".") return 1;
  int res=0;
  std::vector<int> vars(3);
  for (int p=0; p<3; ++p) {
    vars[0] = p;
    for (int q=0; q<3; ++q) {
      vars[1] = q;
      for (int r=0; r<3; ++r) {
        vars[2] = r;
        size_t i=0;
        if (parse(s, i, vars) == 2)
          ++res;
      }
    }
  }
  printf("%d\n", res);
  return 0;
}

int main() {
  while (!testcase_ends()) {}
}

