#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

string str;

int calc(int idx){
    if(str[idx]=='('){
        int depth=1;
        int idx2=idx+1;
        while(depth>0){
            if(str[idx2]=='(') depth++;
            if(str[idx2]==')') depth--;
            if(str[idx2]=='*' && depth==1){
                return min(calc(idx+1),calc(idx2+1));
            }
            if(str[idx2]=='+' && depth==1){
                return max(calc(idx+1),calc(idx2+1));
            }
            idx2++;
        }
    }else{
        if(str[idx]=='-'){
            return 2-calc(idx+1);
        }else{
            return str[idx]-'0';
        }
    }
    return 0;
}

int main(){
    while(1){
        string tmp;
        cin >> tmp;
        if(tmp[0] == '.') break;
        
        str=tmp;
        int count=0;
        for(int i=0; i<=2; i++){
            for(int j=0; j<=2; j++){
                for(int k=0; k<=2; k++){
                    for(int idx=0; idx<(int)tmp.length(); idx++){
                        str[idx] = tmp[idx];
                        if(tmp[idx]=='P') str[idx] ='0'+i;
                        if(tmp[idx]=='Q') str[idx] ='0'+j;
                        if(tmp[idx]=='R') str[idx] ='0'+k;
                    }
                    if(calc(0)==2) count++;
                }
            }
        }
        cout << count << endl;
    }
    return 0;
}