#include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
template<class T>
using Table = vector<vector<T>>;
const ld eps=1e-9;

//// < "D:\D_Download\Visual Studio 2015\Projects\programing_contest_c++\Debug\a.txt"

string st;
int a;
vector<int>nums;
int calc(const int a, const char c, const int b) {
	if (c == '-') {
		return 2 - a;
	}
	else if (c == '*') {
		return min(a, b);
	}
	else if (c == '+') {
		return max(a, b);
	}
	else {
		assert(false);
	}
}
int getnum() {
	if (isdigit(st[a])) {
		int num = st[a] - '0';
		a++;
		return num;
	}
	else {
		int num = nums[st[a] - 'P'];
		a++;
		return num;
	}
}

int getform() {
	if (st[a] == '-') {
		a++;
		int num = getform();
		return calc(num, '-', num);
	}
	else if (st[a] == '(') {
		a++;
		int num = getform();
		char c = st[a];
		a++;
		int anum = getform();
		assert(st[a] == ')');
		a++;
		return calc(num, c, anum);
	}
	else {
		return getnum();
	}
}
int solve() {
	int correct = 0;
	for (int i = 0; i < 27; ++i) {
		{
			int n(i);
			for (int j = 0; j < 3; ++j) {
				nums[j] = n % 3;
				n /= 3;
			}
		}
		a = 0;
		int ans = getform();
		if (ans == 2)correct++;
	}
	return correct;
}
int main() {
	nums.resize(3);
	while (1) {
		cin >> st;
		if (st == ".")break;
		int ans = solve();
		cout << ans << endl;
	}
	return 0;
}