#include <bits/stdc++.h>
using namespace std;

int rev(int x){
    if (x == 0) return 2;
    else if (x == 1) return 1;
    else return 0;
}

int add(int x, int y){
    return max(x, y);
}

int fac(int x, int y){
    return min(x, y);
}

int main(){
    while (1){
        string s;
        cin >> s;
        if (s == ".") break;
        int ans = 0;
        for (int p = 0; p < 3; p++){
            for (int q = 0; q < 3; q++){
                for (int r = 0; r < 3; r++){
                    string t = s;
                    while (t.size() != 1){
                        int left = 0;
                        int right = 0;
                        int k = t.size();
                        for (int i = 0; i < k; i++){
                            if (t[i] == '(') left = i;
                            if (t[i] == ')'){
                                right = i;
                                int dec = 0;
                                char ope;
                                int one;
                                int two;
                                for (int j = left + 1; j < right - 1; j++){
                                    if (t[j] == '-') dec++;
                                    else if (t[j] == '+' || t[j] == '*') ope = t[j];
                                    else {
                                        if (t[j] == 'P') one = p;
                                        else if (t[j] == 'Q') one = q;
                                        else if (t[j] == 'R') one = r;
                                        else one = t[j] - '0';
                                        if (dec % 2 == 1) one = rev(one);
                                        dec = 0;
                                    }
                                }
                                if (t[right - 1] == 'P') two = p;
                                else if (t[right - 1] == 'Q') two = q;
                                else if (t[right - 1] == 'R') two = r;
                                else two = t[right - 1] - '0';
                                if (dec % 2 == 1) two = rev(two);
                                string next = "";
                                if (ope == '+') next += add(one, two) + '0';
                                else next += fac(one, two) + '0';
                                t = t.substr(0, left) + next + t.substr(right+1);
                                break;
                            }
                        }
                        if (left == 0 && right == 0){
                            int dec = 0;
                            int one;
                            for (int i = 0; i < k; i++){
                                if (t[i] == '-') dec++;
                                else {
                                    if (t[i] == 'P') one = p;
                                    else if (t[i] == 'Q') one = q;
                                    else if (t[i] == 'R') one = r;
                                    else one = t[i] - '0';
                                    if (dec % 2 == 1) one = rev(one);
                                    dec = 0;
                                }
                            }
                            t = "";
                            t += one + '0';
                        }
                    }
                    if (t == "P"){
                        t = "";
                        t += p + '0';
                    }
                    if (t == "Q"){
                        t = "";
                        t += q + '0';
                    }
                    if (t == "R"){
                        t = "";
                        t += r + '0';
                    }
                    if (t == "2") ans++;
                }
            }
        }
        cout << ans << endl;
    }
}


Measured memory_bytes: 3228