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

string input;
int p, q, r;

int i;
int fomula();
int inpar();

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

int mul(int left, int right) {
	return min(left, right);
}

int no(int value) {
	return 2 - value;
}

int fomula() {
	switch(input[i++]) {	
	case 'P':
		return p;
	case 'Q':
		return q;
	case 'R':
		return r;
	case '0':
		return 0;
	case '1':
		return 1;
	case '2':
		return 2;
	case '-':
		return no(fomula());
	}

	return inpar();
}

int inpar() {
	int left = fomula();
	char ope = input[i++];
	int right = fomula();
	++i;

	switch(ope) {
	case '+':
		return add(left, right);
	case '*':
		return mul(left, right);
	}	
}

int main() {
	while(cin >> input, input[0] != '.') {
		int count = 0;
		for(p=0; p<3; p++)
			for(q=0; q<3; q++)
				for(r=0; r<3; r++)
					if(i=0, fomula() == 2)
						count++;

		cout << count << endl;
	}

	return 0;
}