#include <bits/stdc++.h>
using namespace std;

#define int long long

string s;
int p = 0;
int P, Q, R;

int dfs() {
  char c = s[p++];
  if ( isdigit(c) ) return c-'0';
  if ( c == 'P' ) return P;
  if ( c == 'Q' ) return Q;
  if ( c == 'R' ) return R;
  if ( c == '-' ) return 2-dfs();
  if ( c == '(' ) {
    int l = dfs();
    char c_ = s[p++];
    int r = dfs();
    p++;    
    if ( c_ == '*' ) return min(l, r);
    if ( c_ == '+' ) return max(l, r);
  }
  return 0;  
}

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);
  
  while ( cin >> s, s != "." ) {
    int cnt = 0;
    for ( P = 0; P <= 2; P++ ) {
      for ( Q = 0; Q <= 2; Q++ ) {
	for ( R = 0; R <= 2; R++ ) {
	  p = 0;
	  if ( dfs() == 2 ) cnt++;
	}
      }
    }
    cout << cnt << endl;  
  }  
       
  return 0;
}

