#include<iostream>
#include<algorithm>
#include<string>
#include<cctype>
#define rep(i,n) for((i)=0;(i)<(n);i++)

using namespace std;

int v[3],n;
string s;

int f(){
  n++;
  if(s[n-1]=='('){
    int res=f();
    n++;
    if(s[n-1]=='+')res=max(res,f());
    if(s[n-1]=='*')res=min(res,f());
    n++;
    return res;
  }
  if(isdigit(s[n-1]))return s[n-1]-'0';
  if(isupper(s[n-1]))return v[s[n-1]-'P'];
  if(s[n-1]=='-')return 2-f();
}

int main(){
  while(cin >> s,s!="."){
    int cnt=0;
    rep(v[0],3)rep(v[1],3)rep(v[2],3)n=0,cnt+=f()/2;
    cout << cnt << endl;
  }
  return 0;
}