#include <bits/stdc++.h>
using namespace std;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
int get_minus(int size, int p) {
  if (p == 1) return 1;
  if (p == 2) {
    if (size % 2 == 0)
      return 2;
    else
      return 0;
  }
  if (size % 2 == 0)
    return 0;
  else
    return 2;
}
int get_plus(int a, int b) {
  if (a == 2 || b == 2) return 2;
  if (a == 1 || b == 1) return 1;
  return 0;
}
int get_mult(int a, int b) {
  if (a == 0 || b == 0) return 0;
  if (a == 1 || b == 1) return 1;
  return 2;
}
int main(void) {
  while (1) {
    string str;
    cin >> str;
    if (str == ".") break;
    stack<char> enzan;
    stack<int> kazu;
    int ans = 0;
    for (int p = 0; p < 3; p++) {
      for (int q = 0; q < 3; q++) {
        for (int r = 0; r < 3; r++) {
          for (int i = 0; i < str.size(); i++) {
            if ('0' <= str[i] && str[i] <= '2') {
              int num = str[i] - '0';
              kazu.push(num);
            } else if (str[i] == 'P')
              kazu.push(p);
            else if (str[i] == 'Q')
              kazu.push(q);
            else if (str[i] == 'R')
              kazu.push(r);
            else if (str[i] == '-') {
              int size = 0;
              while (str[i] == '-') {
                size++;
                i++;
              }
              if ('0' <= str[i] && str[i] <= '2') {
                int num = str[i] - '0';
                kazu.push(get_minus(size, num));
              } else if (str[i] == 'P')
                kazu.push(get_minus(size, p));
              else if (str[i] == 'Q')
                kazu.push(get_minus(size, q));
              else if (str[i] == 'R')
                kazu.push(get_minus(size, r));
            } else if (str[i] == ')') {
              int a = kazu.top();
              kazu.pop();
              int b = kazu.top();
              kazu.pop();
              char c = enzan.top();
              enzan.pop();
              if (c == '*') kazu.push(get_mult(a, b));
              if (c == '+') kazu.push(get_plus(a, b));
            } else if (str[i] == '+')
              enzan.push(str[i]);
            else if (str[i] == '*')
              enzan.push(str[i]);
          }
          if (kazu.top() == 2) ans++;
          while (!kazu.empty()) kazu.pop();
          while (!enzan.empty()) enzan.pop();
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
