/*
	????????????
	????±??????????
	????????\???
*/

#include<stdio.h>

#define SIZE 81	//??\???????????????????????°+1
#define FALSE '0'
#define UNKNOWN '1'
#define TRUE '2'
#define X -1	//?????£???

typedef struct LOGIC Logic;
struct LOGIC{
	char formula[SIZE];	//??\????????????????????????
	char v[3];	//?????°???
};

int Main();
char Calc(Logic *logic);	//???????????????????????????????????£??\???????????????????????????
int Not(int a);	//?????????
int And(int a, int b);	//?????????
int Or(int a, int b);	//?????????

int main(int argc, char *argv[]){
	while( Main() == 0 );
	return 0;
}

int Main(){
	Logic logic;
	int num = 0, count = 0, i = 0;	//????????§??????????????????????????????????????????????????°???
	for( i = 0; i < SIZE; i++ ){ logic.formula[i] = '\0'; }
	if( scanf("%s", logic.formula) != 1 ){ return -1; }
	if( logic.formula[0] == '.' ){ return 1; }
	for( logic.v[0] = FALSE; logic.v[0] <= TRUE; logic.v[0]++ ){
		for( logic.v[1] = FALSE; logic.v[1] <= TRUE; logic.v[1]++ ){
			for( logic.v[2] = FALSE; logic.v[2] <= TRUE; logic.v[2]++ ){
				num = 0;
				count += (Calc(&logic) == TRUE);
			}
		}
	}
	printf("%d\n", count);
	return 0;
}

char Calc(Logic *logic){
	int a[2], sc = 0, i = 0;	//?????????????????°?????????????????????????????????
	char stack[SIZE], s = '\0', ope = '\0';	//?????????????????????????????????????????????????????????????????????
	for( i = 0; i < SIZE; i++ ){ stack[i] = '\0'; }
	for( i = 0; logic->formula[i] != '\0'; i++ ){
		if( 'P' <= logic->formula[i] && logic->formula[i] <= 'R' ){
			stack[sc++] = logic->v[logic->formula[i] - 'P'];
		}else{
			stack[sc++] = logic->formula[i];
		}
		if( logic->formula[i] == ')' ){
			//??????????????????????????§???a[1]????¨?????????????
			for( s = stack[--sc]; s != '+' && s != '*'; s = stack[--sc] ){
				stack[sc+1] = '\0';
				if( 'P' <= s && s <= 'R' ){
					a[1] = logic->v[s-'P'] + '0';
				}else if( '0' <= s && s <= '2' ){
					a[1] = s;
				}else if( s == '-' ){
					a[1] = Not(a[1]);
				}
			}
			stack[sc+1] = '\0';
			ope = stack[sc];
			//'('?????????????????§a[0]????¨?????????????
			for( s = stack[--sc]; stack[sc] != '('; s = stack[--sc] ){
				stack[sc+1] = '\0';
				if( 'P' <= s && s <= 'R' ){
					a[0] = logic->v[s-'P'] + '0';
				}else if( '0' <= s && s <= '2' ){
					a[0] = s;
				}else if( s == '-' ){
					a[0] = Not(a[0]);
				}
			}
			stack[sc+1] = '\0';
			if( ope == '*' ){
				stack[sc++] = And(a[0], a[1]);
			}else if( ope == '+' ){
				stack[sc++] = Or(a[0], a[1]);
			}
		}
	}
	//??\??????????????????????????§??°??????????????§?????????????¨?????????????
	a[0] = stack[--sc];	//?????????????????????
	//?????¨???????????????NOT?????¨???'-'?????????
	for( sc-- ; sc >= 0; sc-- ){
		if( stack[sc] == '-' ){ a[0] = Not(a[0]); }
	}
	stack[++sc] = a[0];
	return stack[0];
}

int Not(int a){
	switch( a ){
		case FALSE:
			return TRUE;
		case UNKNOWN:
			return UNKNOWN;
		case TRUE:
			return FALSE;
	}
	return X;
}

int And(int a, int b){
	if( a == X || b == X ){ return X; }	//?????§?????????
	if( a == FALSE || b == FALSE ){
		return FALSE;
	}else if( a == UNKNOWN || b == UNKNOWN ){
		return UNKNOWN;
	}else if( a == TRUE && b == TRUE ){
		return TRUE;
	}
	return X;
}

int Or(int a, int b){
	if( a == X || b == X ){ return X; }	//?????§?????????
	if( a == TRUE || b == TRUE ){
		return TRUE;
	}else if( a == UNKNOWN || b == UNKNOWN ){
		return UNKNOWN;
	}else if( a == FALSE && b == FALSE ){
		return FALSE;
	}
	return X;
}

Measured memory_bytes: 536