#include <bits/stdc++.h>
int formula(char *);
int min(int, int);
int max(int, int);
int p, q, r;
int position;
int main() {
  int count = 0;
  char str[100000];
  while (scanf("%s", str) != EOF) {
    if (str[0] == '.') break;
    count = 0;
    for (p = 0; p < 3; p++) {
      for (q = 0; q < 3; q++) {
        for (r = 0; r < 3; r++) {
          position = 0;
          if (formula(str) == 2) count++;
        }
      }
    }
    printf("%d\n", count);
  }
  return 0;
}
int formula(char *str) {
  int result;
  if (str[position] == '-') {
    result = 2 - formula(str);
    position++;
  } else if (str[position] == '(') {
    position++;
    result = formula(str);
    if (str[position] == '*') {
      position++;
      result = min(result, formula(str));
    } else if (str[position] == '+') {
      position++;
      result = max(result, formula(str));
    }
  } else {
    if (str[position] >= '0' && str[position] <= '2')
      return str[position] - '0';
    if (str[position] == 'P')
      return p;
    else if (str[position] == 'Q')
      return q;
    else if (str[position] == 'R')
      return r;
  }
}
int min(int x, int y) {
  if (x > y)
    return y;
  else
    return x;
}
int max(int x, int y) {
  if (x > y)
    return x;
  else
    return y;
}
