#include <bits/stdc++.h>
using namespace std;
char NOT[3] = {'2', '1', '0'};
char AND[3][3] = {{'0', '0', '0'}, {'0', '1', '1'}, {'0', '1', '2'}};
char OR[3][3] = {{'0', '1', '2'}, {'1', '1', '2'}, {'2', '2', '2'}};
char s[100];
char s1[100];
char getNumber() {
  char k[100], kLeft[100], kN[100], kNumber = 0, notCounter = 0;
  char ans;
  for (char i = 0; s1[i] != 0; i++) {
    if (s1[i] == '*' || s1[i] == '+') kLeft[kNumber] = s1[i - 1] - '0';
    if (s1[i] == '-')
      notCounter++;
    else if (s1[i] == '(')
      kNumber++;
    else if (s1[i] == '*')
      kN[kNumber] = 0;
    else if (s1[i] == '+')
      kN[kNumber] = 1;
    else if (s1[i] == ')') {
      if (kN[kNumber] == 0) {
        s1[i] = AND[kLeft[kNumber]][s1[i - 1] - '0'];
      } else {
        s1[i] = OR[kLeft[kNumber]][s1[i - 1] - '0'];
      }
      kNumber--;
      ans = s1[i];
    } else {
      if (notCounter % 2 == 1) s1[i] = NOT[s1[i] - '0'];
      ans = s1[i];
      notCounter = 0;
    }
  }
  return ans - '0';
}
char check(char p, char q, char r) {
  for (char i = 0; s[i] != 0; i++) {
    s1[i] = s[i];
    if (s1[i] == 'P') s1[i] = p + '0';
    if (s1[i] == 'Q') s1[i] = q + '0';
    if (s1[i] == 'R') s1[i] = r + '0';
    s1[i + 1] = 0;
  }
  if (getNumber() == 2) return 1;
  return 0;
}
int main() {
  while (1) {
    scanf("%s", s);
    if (s[0] == '.') break;
    char ans = 0;
    for (char p = 0; p < 3; p++)
      for (char q = 0; q < 3; q++)
        for (char r = 0; r < 3; r++) ans += check(p, q, r);
    printf("%d\n", (int)ans);
  }
}
