#include <string>
#include <iostream>

using namespace std;

int p,q,r;

int formula(string & str , int & i){
    int rtn;
    char c = str[i++];
    switch (c){
    case '0':
    case '1':
    case '2':
        rtn = c - '0';
        break;
    case 'P':
        rtn = p;
        break;
    case 'Q':
        rtn = q;
        break;
    case 'R':
        rtn = r;
        break;
    case '-':
        rtn = 2 - formula(str,i);
        break;
    case '(':
        int rhs = formula(str,i);
        //i++; // )
        char op = str[i++];
        //i++;
        int lhs = formula(str,i);
        if(op == '+') rtn = max(rhs,lhs);
        if(op == '*') rtn = min(rhs,lhs);
        i++;
        break;
    }
    return rtn;
}

int main(){
    string str;
    while(cin >> str , str != "."){
        int c=0;
        for(p=0; p<3; p++){
            for(q=0; q<3; q++){
                for(r=0; r<3; r++){
                    int i=0;
                    if(formula(str,i) == 2) c++;
                }
            }
        }
        cout << c << endl;
    }
    return 0;
}