#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

#define rep(i, n)       reps (i, 0, n)
#define reps(i, f, n)   for (int i = f; i < int(n); ++i)

int cur;
string str;

int expr(int P, int Q, int R)
{
    int a, b, op;
    switch (str[cur]) {
        case '0': case '1': case '2': return str[cur++] - '0';
        case 'P': ++cur; return P;
        case 'Q': ++cur; return Q;
        case 'R': ++cur; return R;
        case '-': ++cur; return 2 - expr(P, Q, R);
        default:
            ++cur;
            a = expr(P, Q, R);
            op = str[cur++] == '*';
            b = expr(P, Q, R);
            ++cur;
            if (op) return min(a, b);
            return max(a, b);
    }
}

int main()
{
    while (cin >> str, str != ".") {
        int cnt = 0;
        rep (P, 3) rep (Q, 3) rep (R, 3) {
            cur = 0;
            if (expr(P, Q, R) == 2) ++cnt;
        }
        cout << cnt << endl;
    }
}