#include <bits/stdc++.h>
using namespace std;
void consume(string::const_iterator& begin, int c);
int digit(string::const_iterator& begin);
int formula(string::const_iterator& begin);
void consume(string::const_iterator& begin, int c) {
  if (*begin != c) {
    cout << "expected:" << (char)c << ":input:" << *begin << endl;
  }
  begin++;
}
int P, Q, R;
int digit(string::const_iterator& begin) {
  int ret = *begin;
  begin++;
  return ret - '0';
}
int exp(string::const_iterator& begin) {
  int ret = 0;
  consume(begin, '(');
  int f = formula(begin);
  char op = *begin;
  begin++;
  int n = formula(begin);
  consume(begin, ')');
  if (op == '*') {
    ret = f * n;
    if (ret == 2) return 1;
    if (ret == 4) return 2;
    return ret;
  }
  if (op == '+') {
    ret = f + n;
    cout << "in plus ret = " << ret << endl;
    if (ret == 3 || ret == 4) return 2;
    if (f == 1 && n == 1) return 1;
    return ret;
  }
}
int formula(string::const_iterator& begin) {
  char c = *begin;
  if (isdigit(c)) {
    return digit(begin);
  } else if (c == '(') {
    return exp(begin);
  } else if (c == '-') {
    begin++;
    return 2 - formula(begin);
  } else if (c == 'P') {
    begin++;
    return P;
  } else if (c == 'Q') {
    begin++;
    return Q;
  } else if (c == 'R') {
    begin++;
    return R;
  }
}
int main() {
  cin.sync_with_stdio(false);
  string str;
  while (true) {
    int count = 0;
    getline(cin, str);
    if (str == ".") break;
    for (P = 0; P < 3; P++)
      for (Q = 0; Q < 3; Q++)
        for (R = 0; R < 3; R++) {
          string::const_iterator begin = str.begin();
          if (formula(begin) == 2) {
            count++;
          }
        }
    cout << count << endl;
  }
  return 0;
}
