#include<bits/stdc++.h>
using namespace std;

string target;
int pos;

struct Node{
	string num;
	Node *left;
	Node *right;
	Node(){
		num = "";
		left = NULL;
		right = NULL;
	}
	Node(string n, Node *l, Node *r){
		num = n;
		left = l;
		right = r;
	}
};

//Node *rule_Element();
//Node *rule_Tree();
//string rule_Num();
Node *rule_formula();

Node *rule_formula(){
	Node * buff = new Node;
	switch (target[pos]){
	case '0':
	case '1':
	case '2':
	case 'P':
	case 'Q':
	case 'R':
		buff->num = target[pos];
		pos++;
		break;
	case '-':
		buff->num = target[pos];
		pos++;
		buff->left = rule_formula();
		break;
	case '(':
		pos++;
		buff->left = rule_formula();
		buff->num = target[pos];
		pos++;
		buff->right = rule_formula();
		assert(target[pos] == ')'); pos++;
		break;
	}

	return buff;
}

/*
Node *rule_Tree(){
	Node *buff = new Node;

	buff->left = rule_Element();//to Elem
	assert(target[pos] == '['); pos++;
	buff->num = rule_Num();//to Num
	assert(target[pos] == ']'); pos++;
	buff->right = rule_Element();//to Elem

	return buff;
}

Node *rule_Element(){
	Node *buff = new Node;
	//cout << "pos:" << pos << endl;
	assert(target[pos] == '('); pos++;
	if (target[pos] == ')'){
		pos++;
		return NULL;
	}
	else {
		buff = rule_Tree();//to Tree
		assert(target[pos] == ')'); pos++;
		return buff;
	}
}

string rule_Num(){
	string ret;
	while (target[pos] != ']'){
		ret += target[pos]; pos++;
	}
	return ret;
}
*/

Node *makeTree(string str){

	target = str;
	pos = 0;
	Node *ret;

	ret = rule_formula();

	return ret;
}

string itos(int i) {
	stringstream ss;
	ss << i;
	return ss.str();
}

Node *merge(Node *a, Node *b){
	//cout << "a.num:" << (a->num) << "\tb.num" << (b->num) << endl;
	Node *buff = new Node;
	int v1 = (atoi)(a->num.c_str());
	int v2 = (atoi)(b->num.c_str());
	buff->num = itos(v1 + v2);
	if (a->left != NULL && b->left != NULL) buff->left = merge(a->left, b->left);
	if (a->right != NULL && b->right != NULL) buff->right = merge(a->right, b->right);

	return buff;
}

void out(Node *c){
	cout << "(";
	if (c->left != NULL) out(c->left);
	cout << ")[" << c->num << "](";
	if (c->right != NULL) out(c->right);
	cout << ")";
	return;
}
int eval(Node *n, int p, int q, int r){
	int l_eval, r_eval;
	switch (n->num[0]){
	case '0':
		return 0;
		break;
	case '1':
		return 1;
		break;
	case '2':
		return 2;
		break;
	case 'P':
		return p;
		break;
	case 'Q':
		return q;
		break;
	case 'R':
		return r;
		break;
	case '-':
		l_eval = eval(n->left, p, q, r);
		if (l_eval == 0) return 2;
		else if (l_eval == 1) return 1;
		else return 0;
		break;
	case '*':
		l_eval = eval(n->left, p, q, r);
		r_eval = eval(n->right, p, q, r);
		if (l_eval == 0) return 0;
		else if (r_eval == 0) return 0;
		else if (l_eval == 1) return 1;
		else if (r_eval == 1) return 1;
		else return 2;
		break;
	case '+':
		l_eval = eval(n->left, p, q, r);
		r_eval = eval(n->right, p, q, r);
		if (l_eval == 2) return 2;
		else if (r_eval == 2) return 2;
		else if (l_eval == 1) return 1;
		else if (r_eval == 1) return 1;
		else return 0;
	}
}

int main(void){

	string A;
	while (cin >> A, A[0] != '.'){
		Node *a;

		a = makeTree(A);

		int result = 0;
		for (int i = 0; i < 3; i++){
			for (int j = 0; j < 3; j++){
				for (int k = 0; k < 3; k++){
					if (eval(a, i, j, k) == 2) result++;
				}
			}
		}

		cout << result << endl;
	}

	return 0;
}

Measured memory_bytes: 1384