#include <bits/stdc++.h>

using namespace std;

string s;
int P, Q, R;
int NOT[3] = {2, 1, 0};
int AND[3][3] = {{0, 0, 0}, {0, 1, 1}, {0, 1, 2}};
int OR[3][3] = {{0, 1, 2}, {1, 1, 2}, {2, 2, 2}};

int calc(int& index) {
  int ret;
  if (s[index] == 'P')
    ret = P;
  else if (s[index] == 'Q')
    ret = Q;
  else if (s[index] == 'R')
    ret = R;
  else if (isdigit(s[index])) {
    ret = s[index] - '0';
  } else if (s[index] == '-') {
    return NOT[calc(++index)];
  } else if (s[index] == '(') {
    ret = calc(++index);
    if (s[index] == '*') {
      ret = AND[ret][calc(++index)];
    } else {
      ret = OR[ret][calc(++index)];
    }
  }
  index++;
  return ret;
}

int main() {
  while (cin >> s, s != ".") {
    int ans = 0;
    for (P = 0; P < 3; P++) {
      for (Q = 0; Q < 3; Q++) {
        for (R = 0; R < 3; R++) {
          int c = 0;
          if (2 == calc(c)) ans++;
        }
      }
    }
    cout << ans << "\n";
  }

  return 0;
}
