#include <bits/stdc++.h>
using namespace std;
string str;
int p, q, r;
int negation(int &pt);
int prod(int x, int y);
int disj(int x, int y);
int calc(int &pt);
int getv(char ch);
int getlhs(int &pt);
int getrhs(int &pt);
int solve() {
  int cnt = 0;
  cin >> str;
  if (str == ".") {
    return 0;
  }
  for (p = 0; p < 3; p++)
    for (q = 0; q < 3; q++)
      for (r = 0; r < 3; r++) {
        int ptr = 0;
        if (calc(ptr) == 2) cnt++;
      }
  cout << cnt << endl;
  return 1;
}
int main(void) {
  while (solve()) {
  }
  return 0;
}
int getvalue(int &pt) {
  switch (str[pt]) {
    case '0':
      return 0;
      break;
    case '1':
      return 1;
      break;
    case '2':
      return 2;
      break;
    case 'P':
      return p;
      break;
    case 'Q':
      return q;
      break;
    case 'R':
      return r;
      break;
  }
  return 0;
}
int negation(int &pt) {
  if (str[pt] == '-') {
    pt++;
    return negation(pt);
  }
  if (str[pt] == '(') {
    pt++;
    return 2 - calc(pt);
  }
  int ret = 2 - getvalue(pt);
  pt++;
  return ret;
}
int disj(int x, int y) {
  if (x == y) {
    return x;
  }
  if (x + y > 2) {
    return 2;
  }
  return x + y;
}
int prod(int x, int y) {
  if (x == 0 || y == 0) {
    return 0;
  }
  if (x == 2 && y == 2) {
    return 2;
  }
  return 1;
}
int calc(int &pt) {
  int lhs;
  if (str[pt] == '-') {
    lhs = negation(++pt);
  } else if (str[pt] == '(') {
    lhs = calc(++pt);
  } else {
    lhs = getvalue(pt);
    ++pt;
  }
  if ((pt >= str.length() - 1) || (str[pt] == ')')) {
    ++pt;
    return lhs;
  }
  char op = str[pt];
  ++pt;
  int rhs;
  if (str[pt] == '-') {
    rhs = negation(++pt);
  } else if (str[pt] == '(') {
    rhs = calc(++pt);
  } else {
    rhs = getvalue(pt);
    ++pt;
  }
  switch (op) {
    case '+':
      return disj(lhs, rhs);
      break;
    case '*':
      return prod(lhs, rhs);
      break;
  }
  return -1;
}
