#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 a[3];

int formula(string& s,int& idx){
    if('0'<=s[idx]&&s[idx]<='2'){
        int ret = s[idx]-'0';
        ++idx;
        return ret;
    }
    else if('P'<=s[idx]&&s[idx]<='R'){
        int ret = a[s[idx]-'P'];
        ++idx;
        return ret;
    }
    else if(s[idx]=='-'){
        ++idx;
        return 2-formula(s,idx);
    }
    else if(s[idx]=='('){
        ++idx;
        int lhs = formula(s,idx);
        char op = s[idx];
        ++idx;
        int rhs = formula(s,idx);
        assert(s[idx]==')');
        ++idx;
        if(op == '+')return max(lhs, rhs);
        else return min(lhs, rhs);
    }
    else assert(false);
}

void solve(string& s){
    int ans = 0;
    rep(i,3)rep(j,3)rep(k,3){
        a[0]=i;a[1]=j;a[2]=k;
        int idx = 0;
        ans += formula(s,idx) == 2;
    }
    cout<<ans<<endl;
}

int main(){
    string s;
    while(cin>>s,s!=".")solve(s);
    return 0;
}
