#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <iostream>
#include <set>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define PRIME1 99999883
#define PRIME2 99999893
#define EPS 0.00000001
using namespace std;

char buf[100];

int calc_not(int value){
	switch(value){
	case 0:return 2;
	case 1:return 1;
	case 2:return 0;
	}
	return -1; //must not reach here
}

int calc_and(int left,int right){
	if(left == 0 || right == 0)return 0;
	else{
		if(left == 1 || right == 1)return 1;
		else{
			return 2;
		}
	}
}

int calc_or(int left,int right){
	return max(left,right);
}

int recursive(int P,int Q,int R,int left,int right){
	int depth = 0,op_pos = -1;

	for(int i = left; i <= right; i++){
		if(buf[i] == '(')depth++;
		else if(buf[i] == ')')depth--;
		else if((buf[i] == '+' || buf[i] == '*') && depth == 0){
			op_pos = i;
			break;
		}
	}

	int FLG = 1,index,num;

	if(op_pos == -1){

		for(index = left; buf[index] == '-'; index++){
			FLG *= -1;
		}
		if(buf[index] >= '0' && buf[index] <= '2'){
			num = buf[index] - '0';
			if(FLG == -1){
				return calc_not(num);
			}else{
				return num;
			}
		}else if(buf[index] == 'P' || buf[index] == 'Q' || buf[index] == 'R'){
			switch(buf[index]){
			case 'P':
				num = P;
				break;
			case 'Q':
				num = Q;
				break;
			case 'R':
				num = R;
				break;
			}
			if(FLG == -1){
				return calc_not(num);
			}else{
				return num;
			}
		}else{ //buf[index] == '('
			if(FLG == -1){
				return calc_not(recursive(P,Q,R,index+1,right-1));
			}else{
				return recursive(P,Q,R,index+1,right-1);
			}
		}

	}else{
		int calc_left,calc_right;

		if(buf[op_pos-1] == ')'){
			if(buf[left] == '-'){

				FLG =1;
				for(index = left; buf[index] == '-';index++){
					FLG *= -1;
				}
				if(FLG == -1){
					calc_left = calc_not(recursive(P,Q,R,index+1,op_pos-2));
				}else{
					calc_left = recursive(P,Q,R,index+1,op_pos-2);
				}

			}else{ //buf[left] == '('
				calc_left = recursive(P,Q,R,left+1,op_pos-2);
			}
		}else{
			calc_left = recursive(P,Q,R,left,op_pos-1);
		}

		if(buf[op_pos+1] == '('){
			calc_right = recursive(P,Q,R,op_pos+2,right-1);
		}else{
			if(buf[right] == ')'){
				if(buf[op_pos+1] == '-'){

					FLG =1;
					for(index = op_pos+1; buf[index] == '-';index++){
						FLG *= -1;
					}
					if(FLG == -1){
						calc_right = calc_not(recursive(P,Q,R,index+1,right-1));
					}else{
						calc_right = recursive(P,Q,R,index+1,right-1);
					}

				}else{
					calc_right = recursive(P,Q,R,op_pos+1,right);
				}
			}else{
				calc_right = recursive(P,Q,R,op_pos+1,right);
			}
		}

		if(buf[op_pos] == '+'){
			return calc_or(calc_left,calc_right);
		}else{ //buf[op_pos] == '*'
			return calc_and(calc_left,calc_right);
		}
	}
}

bool purse(int P,int Q,int R){
	int length;
	for(length = 0; buf[length] != '\0';length++);

	int left = -1,right = -1;

	for(int i = 0; i < length; i++){
		if(buf[i] == '('){
			left = i;
			break;
		}
	}

	for(int i = length-1; i >= 0; i--){
		if(buf[i] == ')'){
			right = i;
			break;
		}
	}

	int FLG = 1,index,num;
	if(left == -1){
		for(index = 0; buf[index] == '-'; index++){
			FLG *= -1;
		}
		if(buf[index] >= '0' && buf[index] <= '2'){
			num = buf[index] - '0';
			if(FLG == -1){
				return calc_not(num) == 2;
			}else{
				return num == 2;
			}
		}else{
			switch(buf[index]){
			case 'P':
				num = P;
				break;
			case 'Q':
				num = Q;
				break;
			case 'R':
				num = R;
				break;
			}
			if(FLG == -1){
				return calc_not(num) == 2;
			}else{
				return num == 2;
			}
		}

	}else{
		if(left == 0){
			return recursive(P,Q,R,left+1,right-1) == 2;
		}else{
			for(index = 0; buf[index] == '-'; index++){
				FLG *= -1;
			}
			if(FLG == -1){
				return calc_not(recursive(P,Q,R,index+1,right-1)) == 2;
			}else{
				return recursive(P,Q,R,index+1,right-1) == 2;
			}
		}
	}
}

void func(){

	int count = 0;

	for(int P = 0; P <= 2; P++){
		for(int Q = 0; Q <= 2; Q++){
			for(int R = 0; R <=2; R++){
				if(purse(P,Q,R))count++;
			}
		}
	}

	printf("%d\n",count);
}


int main(){

	while(true){
		scanf("%s",buf);
		if(buf[0] == '.')break;

		func();
	}
    return 0;
}

Measured memory_bytes: 3224