#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <cctype>

using namespace std;
string str;
int p, q, r, pos;

int formula(){
    if (isdigit(str[pos])) {return str[pos++] - '0';}
    if (str[pos] == 'P') {pos++; return p;}
    if (str[pos] == 'Q') {pos++; return q;}
    if (str[pos] == 'R') {pos++; return r;}
    
    int res = 0;
    if (str[pos] == '-') {pos++; return 2 - formula();}
    if (str[pos] == '(') {
        pos++;
        res = formula();
        if (str[pos] == '*') {
            pos++;
            res = min(res, formula());
        }
        else if (str[pos] == '+') {
            pos++;
            res = max(res, formula());
        }
        pos++;
        return res;
    }
}

int main()
{
    while (true) {
        cin >> str;
        if (str == ".") {
            break;
        }
        int cnt = 0;
        for (p = 0; p <= 2; p++) {
            for (q = 0; q <= 2; q++) {
                for (r = 0; r <= 2; r++) {
                    pos = 0;
                    if (formula() == 2) {
                        cnt++;
                    }
                }
            }
        }
        cout << cnt << endl;
    }
    return 0;
}