//=================================
// Created on: 2018/07/04 14:52:47
//=================================
#include <bits/stdc++.h>
using namespace std;
int Not(const int i) { return 2 - i; }
int Plus(const int i, const int j) { return max(i, j); }
int Prod(const int i, const int j) { return min(i, j); }
int v[3] = {0, 0, 0};
string S;
int pos;
int number()
{
    if (isdigit(S[pos])) {
        return pos++, S[pos - 1] - '0';
    } else {
        return pos++, v[S[pos - 1] - 'P'];
    }
}
int formula()
{
    if (isalpha(S[pos]) or isdigit(S[pos])) { return number(); }
    if (S[pos] == '-') {
        pos++;
        const int sub = formula();
        return Not(sub);
    } else {
        pos++;
        const int a = formula();
        const char op = S[pos];
        pos++;
        const int b = formula();
        pos++;
        return (op == '*' ? Prod(a, b) : Plus(a, b));
    }
}

int main()
{
    for (;;) {
        cin >> S;
        if (S == ".") { break; }
        int ans = 0;
        for (v[0] = 0; v[0] < 3; v[0]++) {
            for (v[1] = 0; v[1] < 3; v[1]++) {
                for (v[2] = 0; v[2] < 3; v[2]++) {
                    pos = 0;
                    const int c = formula();
                    if (c == 2) { ans++; }
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}

