#include <bits/stdc++.h>
int OR[3][3] = {{0, 1, 2}, {1, 1, 2}, {2, 2, 2}};
int AND[3][3] = {{0, 0, 0}, {0, 1, 1}, {0, 1, 2}};
int conv(char a, int p, int q, int r) {
  switch (a) {
    case 'P':
      return p;
    case 'Q':
      return q;
    case 'R':
      return r;
    case '0':
      return 0;
    case '1':
      return 1;
    case '2':
      return 2;
  }
}
bool calc(char *s, int P, int Q, int R) {
  std::stack<char> st;
  for (int i = 0; s[i] != '\0'; i++) {
    if (s[i] == ')') {
      int m;
      char rc = st.top();
      st.pop();
      int r = conv(rc, P, Q, R);
      for (m = 0; st.top() == '-'; m++) st.pop();
      if (m % 2) r = 2 - r;
      char o = st.top();
      st.pop();
      char lc = st.top();
      st.pop();
      int l = conv(lc, P, Q, R);
      for (m = 0; st.top() == '-'; m++) st.pop();
      if (m % 2) l = 2 - l;
      st.pop();
      int tmp;
      if (o == '*')
        st.push((char)(AND[r][l] + '0'));
      else
        st.push((char)(OR[r][l] + '0'));
    } else
      st.push(s[i]);
  }
  char ans = st.top();
  st.pop();
  if (!st.empty()) {
    int m;
    for (m = 0; !st.empty(); m++) st.pop();
    if (m % 2) ans = (char)((2 - (ans - '0')) + '0');
  }
  if (ans == '2') return true;
  return false;
}
int solve(char *s) {
  int ans = 0;
  for (int p = 0; p < 3; p++)
    for (int q = 0; q < 3; q++)
      for (int r = 0; r < 3; r++)
        if (calc(s, p, q, r)) ans++;
  return ans;
}
int main() {
  char x[100];
  while (true) {
    std::cin >> x;
    if (x[0] == '.') break;
    std::cout << solve(x) << std::endl;
  }
  return 0;
}
