#include <bits/stdc++.h>
#define rep(i, n) for(int i=0; i<(n); ++i)
#define rep2(i, s, n) for(int i=s; i<(n); ++i)
#define ALL(v) (v).begin(), (v).end()

using namespace std;

typedef long long ll;

template<typename T>
using priority_queue_rev = priority_queue<T, vector<T>, greater<T> >;

static const int INTINF = (2147483647 >> 1); // 10^9 + 5000
static const ll LLINF = (9223372036854775807 >> 1);
static const int MAX = 1e5+1;
static const ll MOD = 1e9+7;

namespace Kunitaka{
    template<
        typename TYPE,
        std::size_t SIZE
    >
    std::size_t array_length(const TYPE (&array)[SIZE])
    {
        return SIZE;
    }

    template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
    template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
}
using namespace Kunitaka;
namespace Printer{
    void br(){
        cout << endl;
    }

    void loop_cnt(int i){
        cout << "i = " << i << endl;
    }

    void loop_cnt(int i, int j){
        cout << "i = " << i << ", " << "j = " << j << endl;
    }

    template<class T>
    void print(T x){
        cout << x << endl;
    }

    template<class T, class S>
    void print(T x, S y){
        cout << x << "," << y << endl;
    }

    template<class T, class S, class U>
    void print(T x, S y, U z){
        cout << x << "," << y << "," << z << endl;
    }

    template<class T, class S>
    void print(pair<T, S> p){
        cout << p.first << ", " << p.second << endl;
    }

    template<
        typename TYPE,
        std::size_t SIZE
    >
    void print(const TYPE (&array)[SIZE]){
        int lim = array_length(array);
        for(int i=0; i<lim; i++){
            if(i) cout << " ";
            cout << array[i];
        }
        cout << endl;
    }

    template<class S>
    void print(vector<S> v){
        for(int i=0; i<v.size(); i++){
            if(i) cout << " ";
            cout<< v[i];
        }
        cout << endl;
    }

    template<class S>
    void print(vector<vector<S> > vv){
        for(int i=0; i<vv.size(); i++){
            for(int j=0; j<vv[i].size(); j++){
                if(j) cout << " ";
                cout << vv[i][j];
            }
            cout << endl;
        }
    }

    void yesno(bool x){
        if(x) cout << "Yes" << endl;
        else cout << "No" << endl;
    }
};
using namespace Printer;

ll hai(ll x){
    if(x == 0) return 2;
    if(x == 1) return 1;
    return 0;
}

ll kake(ll x, ll y){
    if(!x || !y) return 0;
    if(x == 1 || y == 1) return 1;
    return 2;
}

ll plu(ll x, ll y){
    if(!x && !y) return 0;
    if(x == 2 || y == 2) return 2;
    return 1;
}

ll calc(string str){
    int type = 0;
    string a = "";
    string b = "";
    if(str[0] == '-'){
        a = str.substr(1, str.size()-1);
    }
    else
    {
        int cnt = 0;
        for(int i=0; i<str.size(); i++){
            if(str[i] == '('){
                cnt++;
            }
            if(str[i] == ')'){
                cnt--;
            }
            if(cnt == 1){
                if(str[i] == '*'){
                    type = 1;
                    a = str.substr(1, i - 1);
                    b = str.substr(i + 1, str.size() - (i + 1) - 1);
                    // cout << a << " " << b << endl;
                    break;
                }
                else if(str[i] == '+'){
                    type = 2;
                    a = str.substr(1, i - 1);
                    b = str.substr(i + 1, str.size() - (i + 1) - 1);
                    // cout << a << " " << b << endl;
                    break;
                }
            }
        }
    }

    ll res = 0;
    if(type == 0){
        if(isdigit(a[0])){
            res = hai(stoi(a));
        }
        else{
            res = hai(calc(a));
        }
    }
    else if(type == 1){
        ll res_a = 0;
        if(isdigit(a[0])) res_a = stoi(a);
        else res_a = calc(a);
        ll res_b = 0;
        if(isdigit(b[0])) res_b  = stoi(b);
        else res_b = calc(b);
        res = kake(res_a, res_b);
    }
    else{
        ll res_a = 0;
        if(isdigit(a[0])) res_a = stoi(a);
        else res_a = calc(a);
        ll res_b = 0;
        if(isdigit(b[0])) res_b  = stoi(b);
        else res_b = calc(b);
        res = plu(res_a, res_b);
    }
    return res;
}

ll solve(string str){
    ll res = 0;
    string ori = str;
    for(int r=0; r<3; r++){
        for(int p=0; p<3; p++){
            for(int q=0; q<3; q++){
                str = ori;
                for(int i=0; i<str.size(); i++){
                    // static_cast<char>(r + '0')
                    if(str[i] == 'R') str[i]=static_cast<char>(r + '0');
                    else if(str[i] == 'P') str[i]= static_cast<char>(p + '0');
                    else if(str[i] == 'Q') str[i]=static_cast<char>(q + '0');
                }
                // cout << str << endl;

                if(isdigit(str[0])){
                    if(str[0] == '2') res++;
                    continue;
                }
                if(calc(str)==2) res++;
                // cout << res << endl;
            }
        }
    }
    return res;
}

int main(int argc, const char * argv[]) {
    //提出時、消す----//
    //--------------//


    // input
    cout << fixed << setprecision(10);


    // int k = 0;
    while(1){
        string str; cin >> str;
        // cout << k << endl;
        // k++;
        if(str[0] == '.') break;

        // cout << static_cast<char>(1 + '0') << endl;

        cout << solve(str) << endl;
    }

    
    
    return 0;
}



Measured memory_bytes: 3196