#include <bits/stdc++.h>
#define range(i, a, b) for(int i = (a); i < (b); i++)
#define rep(i, a) range(i, 0, a)
using namespace std;

typedef string::const_iterator State;
class ParseError{};

string s;
int P, Q, R;

int formula( State & );
int factor( State & );

int main() {
    while ( cin >> s, s != "." ) {
        int ans = 0;
        rep ( i, 3 ) rep ( j, 3 ) rep ( k, 3 ) {
            P = i, Q = j, R = k;
            State begin = s.begin();
            int tmp = formula( begin );
            ans += (tmp == 2);
        }
        cout << ans << endl;
    }
    return 0;
}


int factor( State & begin ) {
    if ( *begin == '(' ) {
        begin++;
        int ret = formula( begin );
        begin++;
        return ret;
    }
    else if ( *begin == '-' ) {
        begin++;
        return ( 2 - factor( begin ) );
    }
    else {
        int ret = 0;
        if ( '0' <= *begin && *begin <= '2' ) {
            ret = *begin - '0';
            begin++;
        }
        else if ( 'P' <= *begin && *begin <= 'R' ) {
            if ( *begin == 'P' ) ret = P;
            if ( *begin == 'Q' ) ret = Q;
            if ( *begin == 'R' ) ret = R;
            begin++;
        }
        return ret;
    }
}

int formula( State & begin ) {
    int ret = factor( begin );
    if ( *begin == '*' ) {
        begin++;
        ret = min( ret, formula( begin ) );
    }
    else if ( *begin == '+' ) {
        begin++;
        ret = max( ret, formula( begin ) );
    }
    else {

    }
    return ret;
}
