#include <bits/stdc++.h>
using namespace std;
typedef string::const_iterator State;
class ParseError
{
};
int expr(State &begin);
int P, Q, R;
int NOT(int x)
{
    return 2 - x;
}
int AND(int x, int y)
{
    return min(x, y);
}
int OR(int x, int y)
{
    return max(x, y);
}
int main()
{
    string S;
    while (cin >> S)
    {
        if (S == ".")
            return 0;
        int ans = 0;
        for (P = 0; P < 3; P++)
        {
            for (Q = 0; Q < 3; Q++)
            {
                for (R = 0; R < 3; R++)
                {
                    State c = S.begin();
                    if (expr(c) == 2)
                        ans++;
                }
            }
        }
        cout << ans << endl;
    }
}
//パースして、その評価結果を返す。
int expr(State &begin)
{

    int ret;
    if (*begin == 'P')
        ret = P;
    else if (*begin == 'Q')
        ret = Q;
    else if (*begin == 'R')
        ret = R;
    else if (isdigit(*begin))
        ret = *begin - '0';
    else if (*begin == '-')
    {
        begin++;
        return NOT(expr(begin));
    }
    else if (*begin == '(')
    {
        begin++;
        ret = expr(begin);
        if (*begin == '*')
        {
            begin++;
            ret = AND(ret, expr(begin));
        }
        else if (*begin == '+')
        {
            begin++;
            ret = OR(ret, expr(begin));
        }
    }
    begin++;
    return ret;
}

