#include<stdio.h>


int formula();
int min(int,int);
int max(int,int);

int p,q,r;
int position;
    char str[10000];


main(){
    int count=0;
    
    while(1){
        
        scanf("%s",str);
        if(str[0] == '.') break;
        count = 0;
        
    for(p=0; p<3; p++){
        for(q=0; q<3; q++){
            for(r=0; r<3; r++){
                position = 0;
               if(formula() == 2)
               count++;
            }
        }
    }
    printf("%d\n",count);
    }
    
    return 0;
}

int formula(){
    int result;
    
    if(str[position] == '-'){
                position++;

        result = 2-formula();
    }   
    
    else if(str[position] == '('){
    position++;
    result = formula();
    if(str[position] == '*'){
        position++;
        result = min(result,formula());
    position++;
    }
    else if(str[position] == '+'){
        position++;
        result = max(result,formula());
    position++;
	}
}

else{
    if(str[position] >= '0' && str[position] <= '2')
    result = str[position] - '0';
    
    if(str[position] == 'P')
    result = p;
    else if(str[position] == 'Q')
    result = q;
    else if(str[position] == 'R')
    result = r;
    
    position++;
    }
    return result;
}

int min(int x,int y){
    if(x > y)
    return y;
    else return x;
}

int max(int x,int y){
    if(x > y)
    return x;
    else return y;
}