#include <bits/stdc++.h>

class Solve {
private:
	int product[3][3]{
		{0, 0, 0},
		{0, 1, 1},
		{0, 1, 2}
	};
	int sum[3][3]{
		{0, 1, 2},
		{1, 1, 2},
		{2, 2, 2}
	};
	int denial[3]{2, 1, 0};

	std::string formula;
	int variant[3]{};
	int f_i{};

	int calcVal()
	{
		if (formula[f_i] == '(')
			return interpretBinaryOperation();
		else if (formula[f_i] == '-')
		{
			f_i++;
			return denial[calcVal()];
		}
		else if ('P' <= formula[f_i] && formula[f_i] <= 'R')
		{
			f_i++;
			return variant[formula[f_i - 1] - 'P'];
		}
		else
		{
			f_i++;
			return formula[f_i - 1] - '0';
		}
	}

	int interpretBinaryOperation()
	{
		f_i++;
		int left{calcVal()};
		int ope{};
		if (formula[f_i] == '*') ope = 1;
		f_i++;
		int right{calcVal()};
		f_i++;

		if (ope) return product[left][right];
		else return sum[left][right];
	}

public:
	bool is_last_query{};
	Solve()
	{
		std::cin >> formula;
		if (formula[0] == '.')
		{
			is_last_query = true;
			return;
		}
		int count{};
		for (variant[0] = 0; variant[0] < 3; variant[0]++)
			for (variant[1] = 0; variant[1] < 3; variant[1]++)
				for (variant[2] = 0; variant[2] < 3; variant[2]++)
				{
					f_i = 0;
					if (calcVal() == 2)
						count++;
				}
		printf("%d\n", count);
	}

};

int main()
{
	while (!Solve().is_last_query);

	return 0;
}
