#include <bits/stdc++.h>
using namespace std;
int mynot(int i) { return 2 - i; }
int myand(int i, int j) {
  if (i == 2 && j == 2) {
    return 2;
  }
  if (i == 0 || j == 0) {
    return 0;
  }
  return 1;
}
int myor(int i, int j) {
  if (i == 0 && j == 0) {
    return 0;
  }
  if (i == 2 || j == 2) {
    return 2;
  }
  return 1;
}
int interp(int P, int Q, int R, char* formula) {
  char* snd;
  int lcnt = 0;
  int ptr = 0;
  switch (formula[0]) {
    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;
    case '-':
      return mynot(interp(P, Q, R, formula + 1));
      break;
    case '(':
      int left, right;
      ptr++;
      if (formula[ptr] == '(') {
        int cnt = 1;
        ptr++;
        while (cnt != 0) {
          if (formula[ptr] == '(') {
            cnt++;
          }
          if (formula[ptr] == ')') {
            cnt--;
          }
          ptr++;
        }
      } else if (formula[ptr] == '-') {
        while (formula[ptr] == '-') {
          ptr++;
        }
        if (formula[ptr] == '(') {
          int cnt = 1;
          ptr++;
          while (cnt != 0) {
            if (formula[ptr] == '(') {
              cnt++;
            }
            if (formula[ptr] == ')') {
              cnt--;
            }
            ptr++;
          }
        }
        ptr++;
      } else {
        ptr++;
      }
      if (formula[ptr] == '*') {
        left = interp(P, Q, R, formula + 1);
        right = interp(P, Q, R, formula + ptr + 1);
        return myand(left, right);
      }
      if (formula[ptr] == '+') {
        left = interp(P, Q, R, formula + 1);
        right = interp(P, Q, R, formula + ptr + 1);
        return myor(left, right);
      }
  }
}
int main(void) {
  while (1) {
    char formula[80];
    cin >> formula;
    if (formula[0] == '.') return 0;
    int cnt = 0;
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        for (int k = 0; k < 3; k++) {
          if (interp(i, j, k, formula) == 2) cnt++;
        }
      }
    }
    cout << cnt << endl;
  }
}
