#include <iostream>
#include <string>
#include <cassert>
using namespace std;

int p,q,r;
int pos;

int parse(string &s) {
    if(s[pos] == '(') {
        pos++;
        int k = parse(s),res;
        if(s[pos] == '+') {
            pos++;
            int l = parse(s);
            if(k == 2 || l == 2) res = 2;
            else if(k == 0 && l == 0) res = 0;
            else res = 1;
        }else if(s[pos] == '*') {
            pos++;
            int l = parse(s);
            if(k == 0 || l == 0) res = 0;
            else if(k == 2 && l == 2) res = 2;
            else res = 1;
        }
        assert(s[pos] == ')');
        pos++;
        return res;
    }
    if(s[pos] == '-') {
        pos++;
        int k = parse(s);
        if(k == 0) return 2;
        else if(k == 1) return 1;
        else return 0;
    }

    if(s[pos] == 'P') {
        pos++;
        return p;
    }
    if(s[pos] == 'Q') {
        pos++;
        return q;
    }
    if(s[pos] == 'R') {
        pos++;
        return r;
    }
    if(s[pos] == '0' || s[pos] == '1' || s[pos] == '2') {
        int l = s[pos] - '0';
        pos++;
        return l;
    }

    assert(false);
}

int main() {
    string s;
    while(cin>>s) {
        if(s == ".") break;

        int ans = 0;
        for(int i=0; i<=2; ++i)
            for(int j=0; j<=2; ++j)
                for(int k=0; k<=2; ++k) {
                    p=i,q=j,r=k;
                    pos = 0;
                    if(parse(s) == 2) ans++;
                }
        cout<<ans<<endl;
    }
}