//#include <bits/stdc++.h>
//#include <stdio.h>
#include<iostream>
#include<cstdio>
#include<bitset>
#include<algorithm>
#include<vector>
#include<list>
#include<queue>
#include<stack>
#include<string>
#include<string.h>
#include<cmath>
#include<utility>
#include<functional>
#include<map>
#include<set>
#include<cctype>
#include<fstream>


#define FOR(i, a, b) for( int i=(a);i<=(b);i++)
#define RFOR(i, a, b) for( int i=(a);i>=(b);i--)
#define LFOR(i, a, b) for( long long int i=(a);i<=(b);i++)
#define LRFOR(i, a, b) for(long long int i=(a);i>=(b);i--)
#define MOD 1000000007
#define INF 1000000000 //2000000000
#define LLINF 1000000000000000000 //9000000000000000000
#define PI 3.14159265358979
#define MAXI 7500000

using namespace std;
typedef long long int ll;
typedef pair< long long int, long long  int> P;
typedef string::const_iterator State;

int dy[5] = { 0,0,1,-1,0 };
int dx[5] = { 1,-1,0,0 ,0 };


int main(void) {
	
	while (1) {
		int count = 0;
		int calc, first, second;
		int tmp;
		bool flag = false;
		string s;
		

		cin >> s;
		if (s == ".") {
			break;
		}

		FOR(p, 0, 2) {
			FOR(q, 0, 2) {
				FOR(r, 0, 2) {
					flag = false;//true 反転
					stack<int>sta;
					sta.push(-4);

					FOR(i, 0, s.size() - 1) {
						switch (s[i]) {
						case 'P':
							if (flag) {
								flag = false;
								sta.push(-3);
							}
							sta.push(p);
							break;
						case 'Q':
							if (flag) {
								flag = false;
								sta.push(-3);
							}
							sta.push(q);
							break;
						case 'R':
							if (flag) {
								flag = false;
								sta.push(-3);
							}
							sta.push(r);
							break;
						case '0':
							if (flag) {
								flag = false;
								sta.push(-3);
							}
							sta.push(0);
							break;
						case '1':
							if (flag) {
								flag = false;
								sta.push(-3);
							}
							sta.push(1);
							break;
						case '2':
							if (flag) {
								flag = false;
								sta.push(-3);
							}
							sta.push(2);
							break;

						case '(':
							if (flag) {
								flag = false;
								sta.push(-3);
							}

							sta.push(3);
							break;
						case '-':
							if (flag) {
								flag = false;
							}
							else {
								flag = true;
							}
							break;
						case '+':
							sta.push(-1);
							break;
						case '*':
							sta.push(-2);
							break;

						case ')':
							first = sta.top();
							sta.pop();
							tmp = sta.top();

							if (tmp == -3) {
								sta.pop();
								switch (first) {
								case 0:
									first = 2;
									break;
								case 2:
									first = 0;
									break;
								}
							}

							calc = sta.top();
							sta.pop();
							second = sta.top();
							sta.pop();
							tmp = sta.top();

							if (tmp == -3) {
								sta.pop();
								sta.pop();
								switch (second) {
								case 0:
									second = 2;
									break;
								case 2:
									second = 0;
									break;
								}
							}
							else if (tmp == 3) {
								sta.pop();
							}

							switch (calc) {
							case -1:
								if (first == 2 || second == 2) {
									sta.push(2);
								}
								else if (first == 0 && second == 0) {
									sta.push(0);
								}
								else {
									sta.push(1);
								}
								break;
							case -2:
								if (first == 2 && second == 2) {
									sta.push(2);
								}
								else if (first == 0 || second == 0) {
									sta.push(0);
								}
								else {
									sta.push(1);
								}
								break;
							}

							break;
						}

					}

					first = sta.top();
					sta.pop();
					tmp = sta.top();
					if (tmp == -3) {
						switch (first) {
						case 0:
							first = 2;
							break;
						case 2:
							first = 0;
							break;
						}
					}

					if (first == 2) {
						//cout << p << " " << q << " " << r << endl;
						count++;
					}
				}
			}
		}

		cout << count << endl;
	}

	return 0;
}


Measured memory_bytes: 3116