#include <bits/stdc++.h>
using namespace std;
int bar[3] = {2, 1, 0};
int mul[3][3] = {{0, 0, 0}, {0, 1, 1}, {0, 1, 2}};
int add[3][3] = {{0, 1, 2}, {1, 1, 2}, {2, 2, 2}};
string s;
int PQR[3];
bool isvar(char c) {
  return (c - '0' >= 0 && c - '0' <= 2) || (c - 'P' >= 0 && c - 'P' <= 2);
}
int getvar(char c) {
  if (0 <= c - '0' && c - '0' <= 2) return c - '0';
  return PQR[c - 'P'];
}
int rec(int lft, int rgt) {
  int open_cnt = 0, close_cnt = 0, bar_cnt = 0;
  int nxt_lft = -1;
  stack<int> st;
  stack<char> op;
  for (int i = (lft); i < (rgt); ++i) {
    if (open_cnt == close_cnt && s[i] == '-') {
      ++bar_cnt;
    }
    if (open_cnt == close_cnt && isvar(s[i])) {
      int x = getvar(s[i]);
      for (int rep = (0); rep < (bar_cnt); ++rep) x = bar[x];
      st.push(x);
      bar_cnt = 0;
    }
    if (open_cnt == close_cnt && s[i] == '*') op.push('*');
    if (open_cnt == close_cnt && s[i] == '+') op.push('+');
    if (s[i] == '(') {
      if (open_cnt == close_cnt) nxt_lft = i + 1;
      ++open_cnt;
    }
    if (s[i] == ')') {
      ++close_cnt;
      if (open_cnt == close_cnt) st.push(rec(nxt_lft, i));
    }
    if (st.size() >= 2 && !op.empty()) {
      char opr = op.top();
      op.pop();
      int x = st.top();
      st.pop();
      int y = st.top();
      st.pop();
      if (opr == '*') st.push(mul[x][y]);
      if (opr == '+') st.push(add[x][y]);
    }
  }
  return st.top();
}
int main() {
  while (cin >> s) {
    if (s == ".") break;
    int ans = 0;
    for (int p = (0); p < (3); ++p)
      for (int q = (0); q < (3); ++q)
        for (int r = (0); r < (3); ++r) {
          PQR[0] = p;
          PQR[1] = q;
          PQR[2] = r;
          if (rec(0, s.size()) == 2) ++ans;
        }
    cout << ans << endl;
  }
}
