#include <bits/stdc++.h>
using namespace std;
int formula();
void input();
void chkRPar();
int expression();
int calcPlus(int, int);
int calcMul(int, int);
char ch;
int pos;
string str;
int P, Q, R;
int nFlg = 1;
int main(void) {
  int cnt, res;
  while (1) {
    cnt = 0;
    getline(cin, str);
    cout << str << endl;
    for (P = 0; P < 3; P++) {
      for (Q = 0; Q < 3; Q++) {
        for (R = 0; R < 3; R++) {
          pos = 0;
          res = expression();
          if (res == -1) return 0;
          if (res == 2) cnt++;
        }
      }
    }
    cout << cnt << endl;
  }
  return 0;
}
void chkRPar() { input(); }
void input() { ch = str[pos++]; }
int expression() {
  int res = 0;
  input();
  if (ch == '.') return -1;
  switch (ch) {
    case '(':
      res = expression();
      input();
      switch (ch) {
        case '+':
          res = calcPlus(res, expression());
          break;
        case '*':
          res = calcMul(res, expression());
          break;
      }
      chkRPar();
      break;
    default:
      switch (ch) {
        case '-':
          res = 2 - expression();
          break;
        default:
          res = formula();
      }
      break;
  }
  return res;
}
int formula() {
  if (isdigit(ch)) return ch - '0';
  switch (ch) {
    case 'P':
      return P;
    case 'Q':
      return Q;
    case 'R':
      return R;
  }
}
int calcPlus(int d1, int d2) {
  switch (d1) {
    case 0:
      switch (d2) {
        case 0:
          return 0;
        case 1:
          return 1;
        case 2:
          return 2;
      }
    case 1:
      switch (d2) {
        case 0:
          return 1;
        case 1:
          return 1;
        case 2:
          return 2;
      }
    case 2:
      switch (d2) {
        case 0:
          return 2;
        case 1:
          return 2;
        case 2:
          return 2;
      }
  }
}
int calcMul(int d1, int d2) {
  switch (d1) {
    case 0:
      switch (d2) {
        case 0:
          return 0;
        case 1:
          return 0;
        case 2:
          return 0;
      }
    case 1:
      switch (d2) {
        case 0:
          return 0;
        case 1:
          return 1;
        case 2:
          return 1;
      }
    case 2:
      switch (d2) {
        case 0:
          return 0;
        case 1:
          return 1;
        case 2:
          return 2;
      }
  }
}
