#include <iostream>
#include <string>

using namespace std;

struct node_t
{
	virtual int eval(int p, int q, int r) = 0;
};

struct lit : public node_t
{
	const int val;

	lit(int v) : val(v) { }
	int eval(int p, int q, int r) { return val; }
};

struct var : public node_t
{
	const char name;

	var(char name) : name(name) { }
	int eval(int p, int q, int r)
	{
		return name == 'P' ? p : (name == 'Q' ? q : r);
	}
};

struct not : public node_t
{
	node_t *x;

	not(node_t *x) : x(x) { }
	~not()
	{
		delete x;
	}
	int eval(int p, int q, int r)
	{
		return 2 - x->eval(p, q, r);
	}
};

struct or : public node_t
{
	node_t *x, *y;

	or(node_t *x, node_t *y) : x(x), y(y) { }
	~or()
	{
		delete x;
		delete y;
	}
	int eval(int p, int q, int r)
	{
		return max(x->eval(p, q, r), y->eval(p, q, r));
	}
};

struct and : public node_t
{
	node_t *x, *y;

	and(node_t *x, node_t *y) : x(x), y(y) { }
	~and()
	{
		delete x;
		delete y;
	}
	int eval(int p, int q, int r)
	{
		return min(x->eval(p, q, r), y->eval(p, q, r));
	}
};

static string s;
static int p;

bool end() { return p >= s.length(); }
char peek() { return s[p]; }
void succ() { if (!end()) p++; }

node_t *fomula()
{
	char c = peek();
	if ('0' <= c && c <= '2')
	{
		succ();
		return new lit(c - '0');
	}
	else if ('P' <= c && c <= 'R')
	{
		succ();
		return new var(c);
	}
	else if (c == '-')
	{
		succ();
		return new not(fomula());
	}
	else if (c == '(')
	{
		succ();
		node_t *x = fomula();
		char op = peek(); succ();
		node_t *y = fomula();
		succ(); // ')'
		if (op == '+') return new or(x, y);
		else return new and(x, y);
	}
}

node_t *parse_fomula()
{
	p = 0;
	return fomula();
}

int main()
{
	while(cin>>s, s != ".")
	{
		node_t *e = parse_fomula();

		int c = 0;
		for (int p = 0; p <= 2; p++)
		{
			for (int q = 0; q <= 2; q++)
			{
				for (int r = 0; r <= 2; r++)
				{
					if (e->eval(p, q, r) == 2) c++;
				}
			}
		}
		cout << c << endl;

		delete e;
	}
	return 0;
}