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

// region template
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;

ll gcd(ll a, ll b) { return __gcd(a, b); }

ll euclid(ll a, ll b, ll &x, ll &y) {
    if (b) { ll d = euclid(b, a % b, y, x);
        return y -= a/b * x, d; }
    return x = 1, y = 0, a;
}

typedef unsigned long long ull;
typedef long double ld;
ull mod_mul(ull a, ull b, ull M) {
    ll ret = a * b - M * ull(ld(a) * ld(b) / ld(M));
    return ret + M * (ret < 0) - M * (ret >= (ll)M);
}
ull mod_pow(ull b, ull e, ull mod) {
    ull ans = 1;
    for (; e; b = mod_mul(b, b, mod), e /= 2)
        if (e & 1) ans = mod_mul(ans, b, mod);
    return ans;
}

const ll mod = 1000000007;
struct Mod {
    ll x;
    Mod(ll xx) : x(xx) {}
    Mod operator+(Mod b) { return Mod((x + b.x) % mod); }
    Mod operator-(Mod b) { return Mod((x - b.x) % mod); }
    Mod operator*(Mod b) { return Mod((x * b.x) % mod); }
    Mod operator/(Mod b) { return *this * invert(b); }
    Mod invert(Mod a) {
        ll x, y, g = euclid(a.x, mod, x, y);
        assert(g == 1); return Mod((x + mod) % mod);
    }
    Mod operator^(ll e) {
        if (!e) return Mod(1);
        Mod r = *this ^ (e / 2); r = r * r;
        return e & 1 ? *this * r : r;
    }
};
// endregion

struct th {
    int a, b, c;
    const bool operator<(const th& t) const {
        return a != t.a ? a < t.a : b != t.b ? b < t.b : c < t.c;
    }
};

char neg(char x) {
    return '0' + (2 - (x - '0'));
}
char add(char x, char y) {
    return max(x, y);
}
char mul(char x, char y) {
    return min(x, y);
}

int eval(string expr, int p, int q, int r) {
    for (char &c : expr) switch(c) {
        case 'P': c = '0' + p; break;
        case 'Q': c = '0' + q; break;
        case 'R': c = '0' + r; break;
    }
//    cout << expr << endl;
    vector<char> sym;
    for (char c : expr) {
        if (c == '(') sym.push_back(c);
        if (c == ')') sym.erase(sym.end() - 2);
        if (c == '-') {
            if (sym.size() && sym.back() == '-') sym.pop_back();
            else sym.push_back('-');
        }
        if (c == '+') sym.push_back('+');
        if (c == '*') sym.push_back('*');
        if (c == '0' || c == '1' || c == '2') {
            sym.push_back(c);
        }
        bool nomod = false;
        while (!nomod && sym.size() && (sym.back() == '0' || sym.back() == '1' || sym.back() == '2')) {
            char c = sym.back(); sym.pop_back();
            if (sym.size() && sym.back() == '-') sym.pop_back(), c = neg(c);
            if (sym.size() && sym.back() == '+') {
                sym.pop_back();
                c = add(sym.back(), c);
                sym.pop_back();
                sym.push_back(c);
            }
            else if (sym.size() && sym.back() == '*') {
                sym.pop_back();
                c = mul(sym.back(), c);
                sym.pop_back();
                sym.push_back(c);
            }
            else sym.push_back(c), nomod = true;
        }
//        for (char d : sym) cout << d; cout << " -> ";
    }
//    cout << endl;
    return sym.back() - '0';
}

int main() {
    cin.sync_with_stdio(0); cin.tie(0); cin.exceptions(cin.failbit);

    while (true) {
        string s; cin >> s;
        if (s == ".") break;
        int ans = 0;
        rep(p, 0, 3) rep(q, 0, 3) rep(r, 0, 3) ans += eval(s, p, q, r) == 2;
        cout << ans << endl;
    }
}


Measured memory_bytes: 3172