#include <bits/stdc++.h>
using namespace std;
#define rep2(i,a,b) for(int i=a;i<=b;i++)
#define rep(i,n) rep2(i,0,n-1)
#define ll long long
ll INF = 1e9;
const int N = 1e6+100;
int i,ans;
char s[N];
map<char,int> mp;
ll expr();
ll number(){
    if(mp.count(s[i])) return mp[s[i++]];
    else return (s[i++]-'0');
}
ll factor(){
    if(s[i]=='('){
        i++;
        ll res = expr();
        i++;
        return res;
    }
    else return number();
}
ll term(){
    if(s[i]=='-'){
        i++;
        return 2-term();
    }
    else return factor();
}
ll expr(){
    ll res = term();
    for(;;){
        if(s[i]=='+') i++,res = max(res,term());
        else if(s[i]=='*') i++,res = min(res,term());
        else break;
    }
    return res;
}

int main(){
    while(1){
        scanf("%s",s);
        if(s[0]=='.')return 0;
        i=0,ans=0;
        rep(ii,3)rep(j,3)rep(k,3){
            i=0;
            mp['P']=ii;
            mp['Q']=j;
            mp['R']=k;
            if(expr()==2)ans++;
        }
        cout<<ans<<endl;
        for(;i>=0;i--)s[i]=' ';
    }
}
