#include <bits/stdc++.h>
using namespace std;

string S;
int pqr[3];

int getOperator(int l, int r){
    int c = 0;
    for(int i=l;i<r;i++){
        if(c == 0 && (S[i] == '*' || S[i] == '+')){
            return i;
        }else if(S[i] == '('){
            c++;
        }else if(S[i] == ')'){
            c--;
        }
    }
    assert(false);
    return -1;
}

int parseFomula(int l, int r){
    int ret = -1;
    if(S[l] == '('){
        assert(S[r-1] == ')');
        int opPos = getOperator(l+1, r-1);
        int lv = parseFomula(l+1, opPos), rv = parseFomula(opPos+1, r-1);
        if(S[opPos] == '+'){
            ret = max(lv, rv);
        }else{
            ret = min(lv, rv);
        }
    }else if(S[l] == '-'){
        ret = 2 - parseFomula(l+1, r);
    }else if('P' <= S[l] && S[l] <= 'R'){
        ret = pqr[S[l]-'P'];
    }else if('0' <= S[l] && S[l] <= '2'){
        ret = S[l] - '0';
    }
    assert(ret != -1);
    return ret;
}

int main(){
    while(1){
        cin >> S;
        if(S == ".") break;

        int ans = 0;
        for(int i=0;i<=2;i++){
            for(int j=0;j<=2;j++){
                for(int k=0;k<=2;k++){
                    pqr[0] = i;
                    pqr[1] = j;
                    pqr[2] = k;
                    if(parseFomula(0, S.size()) == 2) ans++;
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}