#include<bits/stdc++.h>
using namespace std;
typedef string::const_iterator state;
string s;

int expression(state&);
int number(state &b){
    int ret=*b-'0';
    b++;
    return ret;
}

int factor(state &b){
    if(*b=='('){
        b++;
        int ret=expression(b);
        b++;
        return ret;
    }
    return number(b);
}

int rev(state &b){
    if(*b=='-'){
        b++;
        int ret=rev(b);
        if(ret==0)return 2;
        if(ret==2)return 0;
        return 1;
    }
    return factor(b);
}

int expression(state &b){
    int ret=rev(b);
    while(true){
        if(*b=='+'){
            b++;
            ret=max(ret,rev(b));
        }
        else if(*b=='*'){
            b++;
            ret=min(ret,rev(b));
        }
        else break;
    }
    return ret;
}

int main(){

    string t;
    while(cin>>t,t!="."){
        s=t+"=";

        int ans=0;
        for(int i=0;i<3;i++){
            for(int j=0;j<3;j++){
                for(int k=0;k<3;k++){
                    for(int l=0;l<t.size();l++){
                        if(t[l]=='P')s[l]=i+'0';
                        if(t[l]=='Q')s[l]=j+'0';
                        if(t[l]=='R')s[l]=k+'0';

                    }
                    state b=s.begin();
                    if(expression(b)==2)ans++;

                }
            }
        }
        cout<<ans<<endl;
    }
}