#include <bits/stdc++.h>
using namespace std;
typedef string::const_iterator itr;

int P = 0, Q = 0, R = 0;

int factor(itr &begin);
int num(itr &begin);
int expression(itr &begin);

int factor(itr &begin){
    if(*begin == '('){
        begin++;
        int ret = expression(begin);
        begin++;
        return ret;
    }
    else if(*begin == '-'){
            begin++;
            return 2 - factor(begin);
        }
    else {
        return num(begin);
    }
}

int num(itr &begin){
    int ret = 0;
    if(*begin == 'R') ret = R;
    else if(*begin == 'P') ret = P;
    else if(*begin == 'Q') ret = Q;
    else ret = *begin - '0';
    begin++;
    return ret;
}

int expression(itr &begin){
    int ret = factor(begin);
    for(;;){
        if(*begin == '*'){
            begin++;
            ret = min(ret, factor(begin));
        }
        else if(*begin == '+'){
            begin++;
            ret = max(ret, factor(begin));
        }
        else break;
    }
    return ret;
}

int main(){
    string s; 
    while(true){
        cin >> s;
        if(s == ".") return 0;
        int ans = 0;
        for(R=0; R<=2; R++){
            for(P=0; P<=2; P++){
                for(Q=0; Q<=2; Q++){
                    itr begin = s.begin();
                    int res = expression(begin);
                    if(res == 2) ans++;
                }
            }
        }
        cout << ans << endl;
    }
}
