#include <bits/stdc++.h>
using namespace std;
bool check(string str, int P, int Q, int R) {
  for (int i = 0; i < str.size(); i++) {
    if (str[i] == 'P') str[i] = P + '0';
    if (str[i] == 'Q') str[i] = Q + '0';
    if (str[i] == 'R') str[i] = R + '0';
  }
  stack<int> sta[50];
  int rank = 0;
  for (int i = 0; i < str.size(); i++) {
    if (str[i] == '-') sta[rank].push(-1);
    if (str[i] == '+') sta[rank].push(-2);
    if (str[i] == '*') sta[rank].push(-3);
    if (str[i] == '(') rank++;
    if (str[i] == ')') {
      int c = sta[rank].top(), flag2 = 0;
      sta[rank].pop();
      while (sta[rank].size()) {
        int qd = sta[rank].top();
        sta[rank].pop();
        if (qd == -1) c = 2 - c;
        if (qd == -2) flag2 = 1;
        if (qd == -3) flag2 = 2;
        if (qd >= 0) {
          if (flag2 == 1) c = max(c, qd);
          if (flag2 == 2) c = min(c, qd);
        }
      }
      rank--;
      sta[rank].push(c);
    }
    if (str[i] >= '0' && str[i] <= '2') {
      int c = str[i] - '0', flag2 = 0;
      while (sta[rank].size()) {
        int qd = sta[rank].top();
        sta[rank].pop();
        if (qd == -1) c = 2 - c;
        if (qd == -2) flag2 = 1;
        if (qd == -3) flag2 = 2;
        if (qd >= 0) {
          if (flag2 == 1) c = max(c, qd);
          if (flag2 == 2) c = min(c, qd);
        }
      }
      sta[rank].push(c);
    }
  }
  if (sta[0].top() == 2) return true;
  return false;
}
int main(void) {
  while (1) {
    string str;
    cin >> str;
    if (str == ".") break;
    int cnt = 0;
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        for (int k = 0; k < 3; k++) {
          if (check(str, i, j, k)) cnt++;
        }
      }
    }
    printf("%d\n", cnt);
  }
  return 0;
}
