#include <bits/stdc++.h>
using namespace std;
int P, Q, R;
string str, tmpStr;
int myNot[] = {2, 1, 0};
int myAnd[3][3] = {{0, 0, 0}, {0, 1, 1}, {0, 1, 2}};
int myOr[3][3] = {{0, 1, 2}, {1, 1, 2}, {2, 2, 2}};
vector<int> ans_list;
bool solve(int num) {
  for (int i = num + 1; i < tmpStr.length(); i++) {
    if (tmpStr[i] == '(') {
      solve(i);
      break;
    } else if (tmpStr[i] == ')') {
      int mai = 0;
      int left = -1;
      int right = -1;
      int ope = -1;
      for (int k = num + 1; k < i; k++) {
        if (tmpStr[k] == '-')
          mai++;
        else if (tmpStr[k] == '*')
          ope = 1;
        else if (tmpStr[k] == '+')
          ope = 2;
        else {
          mai %= 2;
          int tmp = tmpStr[k] - '0';
          if (mai == 1) tmp = myNot[tmp];
          if (left == -1)
            left = tmp;
          else
            right = tmp;
          mai = 0;
        }
      }
      int tmp = -1;
      if (ope == 1)
        tmp = myAnd[left][right];
      else
        tmp = myOr[left][right];
      tmpStr.erase(num, i - num + 1);
      tmpStr.insert(tmpStr.begin() + num, tmp + '0');
      break;
    }
  }
  if (tmpStr.length() == 1) {
    if (tmpStr == "2")
      return true;
    else
      return false;
  }
  return false;
}
int main() {
  while (true) {
    cin >> str;
    if (str == ".") break;
    int ans = 0;
    for (int i = 0; i < 27; i++) {
      P = i % 3;
      Q = (i / 3) % 3;
      R = (i / 9) % 3;
      tmpStr.clear();
      tmpStr = str;
      for (int i = 0; i < tmpStr.length(); i++) {
        if (tmpStr[i] == 'P')
          tmpStr[i] = P + '0';
        else if (tmpStr[i] == 'Q')
          tmpStr[i] = Q + '0';
        else if (tmpStr[i] == 'R')
          tmpStr[i] = R + '0';
      }
      while (true) {
        if (tmpStr.length() != 1)
          solve(0);
        else
          break;
      }
      if (solve(0)) ans++;
    }
    ans_list.push_back(ans);
  }
  for (int i = 0; i < ans_list.size(); i++) {
    cout << ans_list[i] << endl;
  }
  return 0;
}
