#define _USE_MATH_DEFINES
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <cfloat>
#include <climits>
#include <cstring>
#include <cmath>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge { int u, v; ll w; };

ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;

int calc(string& s, int& i) {
	if (s[i] == '-') {
		i++;
		return 2 - calc(s, i);
	}
	if (s[i] != '(') return s[i++] - '0';
	i++;
	int X = calc(s, i);
	char c = s[i];
	i++;
	int Y = calc(s, i);
	i++;
	if (c == '*') return min(X, Y);
	if (c == '+') return max(X, Y);
}

int main() {
	for (;;) {
		string s; cin >> s;
		if (s == ".") break;
		int cnt = 0;
		for (int P = 0; P <= 2; P++)
			for (int Q = 0; Q <= 2; Q++)
				for (int R = 0; R <= 2; R++) {
					string _s = s;
					for (int i = 0; i < _s.length(); i++)
						if (_s[i] == 'P') _s[i] = '0' + P;
						else if (_s[i] == 'Q') _s[i] = '0' + Q;
						else if (_s[i] == 'R') _s[i] = '0' + R;
					int i = 0;
					if (calc(_s, i) == 2) cnt++;
				}
		cout << cnt << endl;
	}
}