#include <iostream>
#include <string>
#include <cassert>
using namespace std;

string S;
int cur = 0;

int P,Q,R;

int formula() {
  if (S[cur] == '0' || S[cur] == '1' || S[cur] == '2') {
    return S[cur++] - '0';
  } else if (S[cur] == 'P') {
    cur++;
    return P;
  } else if (S[cur] == 'Q') {
    cur++;
    return Q;
  } else if (S[cur] == 'R') {
    cur++;
    return R;
  } else if (S[cur] == '-') {
    cur++;
    return 2-formula();
  } else {
    assert(S[cur] == '(');
    cur++;
    int a = formula();
    char op = S[cur++];
    int b = formula();
    assert(S[cur] == ')');
    cur++;
    if (op == '*') {
      if (a == 2 && b == 2) {
	return 2;
      } else if (a == 0 || b == 0) {
	return 0;
      } else {
	return 1;
      }
    } else {
      assert(op == '+');
      if (a == 0 && b == 0) {
	return 0;
      } else if (a == 2 || b == 2) {
	return 2;
      } else {
	return 1;
      }      
    }
  }
}

int main() {
  while(cin >> S && S != ".") {
    int ans = 0;
    for(P = 0; P <= 2; P++) {
      for(Q = 0; Q <= 2; Q++) {
	for(R = 0; R <= 2; R++) {
	  cur = 0;
	  int aa = formula();
	  assert(cur == (int)S.length());
	  if (aa == 2) {
	    ans++;
	  }
	}
      }
    }
    cout << ans << endl;
  }
}