#include <bits/stdc++.h>
using namespace std;
int NOT(int x) { return 2 - x; }
int AND(int x, int y) { return min(x, y); }
int OR(int x, int y) { return max(x, y); }
bool is_operator(char op) { return (op == '-' || op == '*' || op == '+'); }
string postfix(string fml) {
  string res = "";
  stack<char> st;
  queue<char> que;
  for (int i = 0; i < (int)(fml.size()); ++i) {
    if (is_operator(fml[i])) {
      while (!st.empty()) {
        if (!is_operator(st.top())) break;
        if (fml[i] == '-') break;
        que.push(st.top());
        st.pop();
      }
      st.push(fml[i]);
    } else if (fml[i] == '(') {
      st.push(fml[i]);
    } else if (fml[i] == ')') {
      while (!(st.top() == '(')) {
        que.push(st.top());
        st.pop();
      }
      st.pop();
    } else {
      que.push(fml[i]);
    }
  }
  while (!que.empty()) {
    res += que.front();
    que.pop();
  }
  return res;
}
int solve(int p, int q, int r, string fml) {
  int res = 0;
  stack<int> st;
  for (int i = 0; i < (int)(fml.size()); ++i) {
    if (fml[i] == 'P') {
      st.push(p);
    } else if (fml[i] == 'Q') {
      st.push(q);
    } else if (fml[i] == 'R') {
      st.push(r);
    } else if (isdigit(fml[i])) {
      st.push(fml[i] - '0');
    } else if (fml[i] == '-') {
      int x = st.top();
      st.pop();
      st.push(NOT(x));
    } else if (fml[i] == '*') {
      int x = st.top();
      st.pop();
      int y = st.top();
      st.pop();
      st.push(AND(x, y));
    } else if (fml[i] == '+') {
      int x = st.top();
      st.pop();
      int y = st.top();
      st.pop();
      st.push(OR(x, y));
    }
  }
  return (st.top() == 2) ? 1 : 0;
}
int main() {
  string formula;
  while (cin >> formula) {
    if (formula == ".") break;
    formula = postfix(formula);
    int ans = 0;
    for (int p = 0; p < (int)(3); ++p) {
      for (int q = 0; q < (int)(3); ++q) {
        for (int r = 0; r < (int)(3); ++r) {
          ans += solve(p, q, r, formula);
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
