#include <bits/stdc++.h>
using namespace std;
queue<char> convert(string formula) {
  queue<char> qu;
  stack<char> st;
  bool b_minus = false;
  for (char c : formula) {
    switch (c) {
      case '(':
        if (b_minus == true) {
          st.push('-');
          b_minus = false;
        }
        break;
      case '0':
      case '1':
      case '2':
      case 'P':
      case 'Q':
      case 'R':
        qu.push(c);
        if (b_minus == true) {
          qu.push('-');
          b_minus = false;
        }
        break;
      case '*':
      case '+':
        st.push(c);
        break;
      case '-':
        b_minus = true;
        break;
      case ')':
        qu.push(st.top());
        st.pop();
        if (!st.empty() && st.top() == '-') {
          qu.push(st.top());
          st.pop();
        }
        break;
    }
  }
  return qu;
}
int lAnd(int a, int b) {
  if (a == 0 || b == 0)
    return 0;
  else if (a == 2 && b == 2)
    return 2;
  else
    return 1;
}
int lOr(int a, int b) {
  if (a == 2 || b == 2)
    return 2;
  else if (a == 0 && b == 0)
    return 0;
  else
    return 1;
}
int lNot(int a) {
  if (a == 0)
    return 2;
  else if (a == 1)
    return 1;
  else
    return 0;
}
int cal(queue<char> quc, int p, int q, int r) {
  stack<int> stc;
  int a, b;
  while (!quc.empty()) {
    char c = quc.front();
    quc.pop();
    switch (c) {
      case '0':
      case '1':
      case '2':
        stc.push(c - '0');
        break;
      case 'P':
        stc.push(p);
        break;
      case 'Q':
        stc.push(q);
        break;
      case 'R':
        stc.push(r);
        break;
      case '+':
        b = stc.top();
        stc.pop();
        a = stc.top();
        stc.pop();
        stc.push(lOr(a, b));
        break;
      case '*':
        b = stc.top();
        stc.pop();
        a = stc.top();
        stc.pop();
        stc.push(lAnd(a, b));
        break;
      case '-':
        a = stc.top();
        stc.pop();
        stc.push(lNot(a));
        break;
    }
  }
  return stc.top();
}
int main(void) {
  string d = "";
  int ans;
  while (true) {
    cin >> d;
    if (d == ".") break;
    d = regex_replace(d, std::regex("(--)"), "");
    queue<char> t = convert(d);
    ans = 0;
    for (int i = 0; i <= 2; i++) {
      for (int j = 0; j <= 2; j++) {
        for (int k = 0; k <= 2; k++) {
          if (cal(t, i, j, k) == 2) ans++;
        }
      }
    }
    cout << ans << endl;
  }
}
