#include <bits/stdc++.h>
using namespace std;
char form[100];
char str[100];
int calc_form(int l, int r) {
  int former, latter;
  int count = 0;
  int i;
  if (str[l] == '0') return 0;
  if (str[l] == '1') return 1;
  if (str[l] == '2') return 2;
  if (str[l] == '-') {
    if ((l - r) % 2 == 0) {
      return str[r] - '0';
    } else {
      int a = str[r] - '0';
      if (a == 0)
        return 2;
      else if (a == 2)
        return 0;
      else
        return 1;
    }
  }
  for (i = l + 1; i < r; i++) {
    if ((str[i] == '+' || str[i] == '*') && count == 0) break;
    if (str[i] == '(') count++;
    if (str[i] == ')') count--;
  }
  former = calc_form(l + 1, i - 1);
  latter = calc_form(i + 1, r - 1);
  if (str[i] == '*') {
    if (former == 2 && latter == 2) return 2;
    if (former == 0 || latter == 0)
      return 0;
    else
      return 1;
  } else if (str[i] == '+') {
    if (former == 2 || latter == 2) return 2;
    if (former == 0 && latter == 0)
      return 0;
    else
      return 1;
  }
}
int main() {
  while (1) {
    cin >> form;
    if (form[0] == '.') break;
    strcpy(str, form);
    int ans = 0;
    for (int p = 0; p <= 2; p++) {
      for (int q = 0; q <= 2; q++) {
        for (int r = 0; r <= 2; r++) {
          for (int i = 0; i < strlen(str); 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';
          }
          if (calc_form(0, strlen(str) - 1) == 2) ans++;
          strcpy(str, form);
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
