#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<functional>
#include<assert.h>
#include<numeric>
using namespace std;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
using ll = long long;
const int inf=1e9+7;
const ll longinf=1LL<<60 ;
const ll mod=1e9+7 ;
int solve2(string& s,int& i);
int solve(string& s,int& i){
    int ret=solve2(s,i);
    while(s[i]=='*'||s[i]=='+'){
        if(s[i]=='*'){
            ret=min(ret,solve2(s,++i));
        }
        else {
            ret=max(ret,solve2(s,++i));
        }
    }
    return ret;
}

int solve2(string& s,int& i){
    if('0'<=s[i]&&s[i]<='2'){
        return s[i++]-'0';
    }
    if(s[i]=='-')return 2-solve2(s,++i);
    ++i;
    int ret=solve(s,i);
    ++i;
    return ret;
}


void solve3(string& s){
    int ans=0;
    rep(i1,3)rep(i2,3)rep(i3,3){
        string t=s;
        rep(j,s.size()){
            if(t[j]=='P')t[j]=i1+'0';
            if(t[j]=='Q')t[j]=i2+'0';
            if(t[j]=='R')t[j]=i3+'0';
        }
        int k=0;
        int ret=solve(t,k);
        if(ret==2)++ans;
    }
    cout<<ans<<endl;
}
int main(){
    string s;
    while(cin>>s,s!=".")solve3(s);
    return 0;
}
