#include<iostream>
#include<cstdio>
#include<string>
#include <cctype>
#include <cassert>
using namespace std;
 
typedef int value;
 
string s;
size_t cur = 0;
value P, Q, R;
int ans = 0;

value not();
value and(value a, value b);
value or(value a, value b);
value formula();
 
value not(){
    assert(s[cur] == '-');
    cur++;
    return 2 - formula();
}
value and(value a, value b){
    return (a<b) ? a : b;
}
value or(value a, value b){
    return (a<b) ? b : a;
}
value formula(){
    value a, b, f;
    char op;
    switch (s[cur]){
    case ('0') :
    case ('1') :
    case ('2') :
               f = s[cur] - '0';
        cur++;
        break;
    case ('P') :
        f = P;
        cur++;
        break;
    case ('Q') :
        f = Q;
        cur++;
        break;
    case ('R') :
        f = R;
        cur++;
        break;
    case ('-') :
        f = not();
        break;
    case ('(') :
        cur++;
        a = formula();
        assert(s[cur] == '*' || s[cur] == '+');
        op = s[cur];
        cur++;
        b = formula();
        f = (op == '*') ? and(a, b) : or(a, b);
        assert(s[cur] == ')');
        cur++;
        break;
    default:
        assert(false);
        break;
    }
    return f;
}
int main(){
    cin >> s;
    while (s != "."){
        for (P = 0; P <= 2; P++)
            for (Q = 0; Q <= 2; Q++)
                for (R = 0; R <= 2; R++){
                    if (formula() == 2)ans++;
                    cur = 0;
                }
        cout << ans << endl;
        ans = 0;
        cin >> s;
    }
    return 0;
}