#include <iostream>
#include <algorithm>
#include <cmath>
#include <cctype>
using namespace std;
int pos, p, q, r;
string str;

int term();
int factor();
int number();

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 count = 0;
        for (p = 0; p <= 2; p++) {
            for (q = 0; q <= 2; q++) {
                for (r = 0; r <= 2; r++) {
                    pos = 0;
                    if (formula() == 2) {
                        count++;
                    }
                }
            }
        }
        cout << count << endl;
    }

    return 0;
}