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

//#define DEBUG
#ifdef DEBUG
#define dump(x) cout << "[*] " #x ": " << x << endl
#define debug(x) x
#else
#define dump(x)
#define debug(x)
#endif

string form;

constexpr int val_0 = 100;
constexpr int val_1 = 200;
constexpr int val_2 = 300;
constexpr int val_p = 400;
constexpr int val_q = 500;
constexpr int val_r = 600;
constexpr int val_minus = 700;

int mns(int x) {
    switch (x) {
        case 0:
            return 2;
        case 1:
            return 1;
        case 2:
            return 0;
    }
}

int mul(int x, int y) {
    if (!x || !y) {
        return 0;
    } else if (x == 1 || y == 1) {
        return 1;
    } else {
        return 2;
    }
}

int add(int x, int y) {
    if (x == 2 || y == 2) {
        return 2;
    } else if (x == 1 || y == 1) {
        return 1;
    } else {
        return 0;
    }
}

function<int(int, int, int)> eval(string s, const vector<int>& t, const int t_off) {
    dump(s);
    dump(t_off);
    if (s[0] == '(') {
        int num = t[t_off];
        int index;
        for (index = 1; index < s.size(); index++) {
            if (t[t_off + index] == num) {
                break;
            }
        }
        dump(num);
        dump(index);
        const auto func = s[index] == '*' ? mul : add;
        const auto left = eval(s.substr(1, index-1), t, t_off+1);
        const auto right = eval(s.substr(index+1, s.size()-index-2), t, t_off+index+1);
        return [func, left, right](int p, int q, int r) { return func(left(p, q, r), right(p, q, r)); };
    } else if (s[0] == '-') {
        const auto func = eval(s.substr(1, s.size()), t, t_off+1);
        return [func](int p, int q, int r) { return mns(func(p, q, r)); };
    } else {
        switch (s[0]) {
            case '0':
                return [](int p, int q, int r) {return 0;};
            case '1':
                return [](int p, int q, int r) {return 1;};
            case '2':
                return [](int p, int q, int r) {return 2;};
            case 'P':
                return [](int p, int q, int r) {return p;};
            case 'Q':
                return [](int p, int q, int r) {return q;};
            case 'R':
                return [](int p, int q, int r) {return r;};
        }
    }
}

int main() {
    while (true) {
        cin >> form;
        if (form[0] == '.') {
            break;
        }
        vector<int> token(form.size());
        int cnt = 0;
        for (int i = 0; i < form.size(); ++i) {
            switch (form[i]) {
                case '(':
                    token[i] = ++cnt;
                    break;
                case ')':
                    token[i] = cnt--;
                    break;
                case '0':
                    token[i] = val_0;
                    break;
                case '1':
                    token[i] = val_1;
                    break;
                case '2':
                    token[i] = val_2;
                    break;
                case 'P':
                    token[i] = val_p;
                    break;
                case 'Q':
                    token[i] = val_q;
                    break;
                case 'R':
                    token[i] = val_r;
                    break;
                case '-':
                    token[i] = val_minus;
                    break;
                case '*':
                    token[i] = cnt;
                    break;
                case '+':
                    token[i] = cnt;
                    break;
            }
        }
        dump("tokenaized");

        cnt = 0;
        auto func = eval(form, token, 0);
        dump("evaled");
        for (int q = 0; q <= 2; ++q) {
            for (int p = 0; p <= 2; ++p) {
                for (int r = 0; r <= 2; ++r) {
                    const int res = func(p, q, r);
                    if (res == 2) {
                        cnt++;
                    }
                }
            }
        }
        cout << cnt << endl;
    }
    return 0;
}



Measured memory_bytes: 3192