#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<int(n);++i)

// <formula> ::= 0 | 1 | 2 | P | Q | R |
// -<formula> | (<formula>*<formula>) | (<formula>+<formula>)

int eval(string &s,int &i,int P,int Q,int R){
    char c = s[i];
    i++;
    if(isdigit(c)) return c-'0';
    if(isalpha(c)) return  c=='P' ? P : c=='Q' ? Q : R;
    if(c == '-') return 2-eval(s,i,P,Q,R);
    int lv = eval(s,i,P,Q,R);
    char op = s[i];
    i++;
    int rv = eval(s,i,P,Q,R);
    i++;
    if(op == '*'){
        if(lv*rv == 0) return 0;
        if(lv*rv == 4) return 2;
        return 1;
    }
    if(op == '+'){
        if(lv+rv == 0) return 0;
        if(lv== 2 || rv == 2) return 2;
        return 1;
    }
    assert(false);
}

int main(void){
    string s;
    while(cin>>s, s!="."){
        int cnt = 0;
        rep(p,3)rep(q,3)rep(r,3){
            int i = 0;
            if(eval(s,i,p,q,r)==2) cnt++;
        }
        cout<<cnt<<endl;
    }
    return 0;
}

