#include <bits/stdc++.h>
const int INF = 1 << 24;
const int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1},
          dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};
int Expr(std::string::const_iterator &);
int Num(std::string::const_iterator &begin) { return *(begin++) - '0'; }
int Not(std::string::const_iterator &begin) {
  if (*begin == '-') {
    begin++;
    int n = Not(begin);
    return 2 - n;
  }
  return Num(begin);
}
int Fact(std::string::const_iterator &begin) {
  if (*begin == '(') {
    begin++;
    int n = Expr(begin);
    begin++;
    return n;
  }
  return Not(begin);
}
int Term(std::string::const_iterator &begin) {
  int res = Fact(begin);
  if (*begin == '*') {
    begin++;
    res = std::min(res, Fact(begin));
  }
  return res;
}
int Expr(std::string::const_iterator &begin) {
  int res = Term(begin);
  if (*begin == '+') {
    begin++;
    res = std::max(res, Fact(begin));
  }
  return res;
}
void position(std::string &s, char c, std::vector<int> &v) {
  int pos = 0;
  while (pos = s.find(c, pos), pos != std::string::npos) {
    v.push_back(pos);
    pos += 1;
  }
}
int main() {
  std::string s;
  while (std::cin >> s, s != ".") {
    std::vector<int> p_pos, q_pos, r_pos;
    position(s, 'P', p_pos);
    position(s, 'Q', q_pos);
    position(s, 'R', r_pos);
    int res = 0;
    for (int p = (0); p < (3); p++) {
      for (int q = (0); q < (3); q++) {
        for (int r = (0); r < (3); r++) {
          std::string t = s;
          for (int i = (0); i < (p_pos.size()); i++) {
            t[p_pos[i]] = p + '0';
          }
          for (int i = (0); i < (q_pos.size()); i++) {
            t[q_pos[i]] = q + '0';
          }
          for (int i = (0); i < (r_pos.size()); i++) {
            t[r_pos[i]] = r + '0';
          }
          std::string::const_iterator state = t.begin();
          if (Expr(state) == 2) {
            res++;
          }
        }
      }
    }
    std::cout << res << std::endl;
  }
}
