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

string s;
int p, q, r;

int not_[3] = {2, 1, 0};
int and_[3][3] = {{0, 0, 0}, {0, 1, 1}, {0, 1, 2}};
int or_[3][3] = {{0, 1, 2}, {1, 1, 2}, {2, 2, 2}};

int f(int &i) {
    int t, x, y;
    char op;
    switch (s[i++]) {
        case '0':
            return 0;
        case '1':
            return 1;
        case '2':
            return 2;
        case 'P':
            return p;
        case 'Q':
            return q;
        case 'R':
            return r;
        case '-':
            return not_[f(i)];
        case '(':
            x = f(i);
            op = s[i++];
            y = f(i);
            i ++;
            return (op == '*') ? and_[x][y] : or_[x][y];
    }
}

int main() {
    while (cin >> s) {
        if (s == ".") break;
        int cnt = 0;
        for (p=0; p<3; ++p)
            for (q=0; q<3; ++q)
                for (r=0; r<3; ++r) {
                    int i = 0;
                    if (f(i) == 2) cnt ++;
                }
        cout << cnt << endl;
    }
    return 0;
}
