#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <algorithm>
#include <functional>
#include <stack>

using namespace std;

#define rep(i,n) for((i)=0;(i)<(int)(n);(i)++)
#define foreach(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)

string in;

int dfs(int l, int r, int P, int Q, int R){
  int N = in.length(),i,res=0;

  if(l + 1 == r){
    if(in[l] == '0' || in[l] == '1' || in[l] == '2') return in[l]-'0';
    if(in[l] == 'P') return P;
    if(in[l] == 'Q') return Q;
    if(in[l] == 'R') return R;
  }
  if(in[l] == '-'){
    return 2 - dfs(l+1,r,P,Q,R);
  }
  if(in[l] == '('){
    int op = 0;
    for(i = l+1; i < r-1; i++){
      if(in[i] == '(') op++;
      if(in[i] == ')') op--;
      if(op == 0 && (in[i] == '*')) return min(dfs(l+1,i,P,Q,R),dfs(i+1,r-1,P,Q,R));
      if(op == 0 && (in[i] == '+')) return max(dfs(l+1,i,P,Q,R),dfs(i+1,r-1,P,Q,R));
    }
  }
}

int main(){
  int i;

  while(1){
    cin >> in;
    if(in == ".") break;

    int j,k,ans=0;
    rep(i,3) rep(j,3) rep(k,3){
      if(dfs(0,in.length(),i,j,k) == 2) ans++;
    }

    cout << ans << endl;
  }

  return 0;
}