#include <bits/stdc++.h>
using namespace std;
int parse(string &T, int &pos){
  if (T[pos] == '0'){
    pos++;
    return 0;
  } else if (T[pos] == '1'){
    pos++;
    return 1;
  } else if (T[pos] == '2'){
    pos++;
    return 2;
  } else if (T[pos] == '-'){
    pos++;
    int A = parse(T, pos);
    return 2 - A;
  } else {
    pos++;
    int A = parse(T, pos);
    char c = T[pos];
    pos++;
    int B = parse(T, pos);
    pos++;
    if (c == '*'){
      return min(A, B);
    }
    if (c == '+'){
      return max(A, B);
    }
  }
}
int main(){
  while (1){
    string S;
    cin >> S;
    if (S == "."){
      break;
    }
    int N = S.size();
    int ans = 0;
    for (int i = 0; i < 3; i++){
      for (int j = 0; j < 3; j++){
        for (int k = 0; k < 3; k++){
          string T = S;
          for (int l = 0; l < N; l++){
            if (T[l] == 'P'){
              T[l] = '0' + i;
            }
            if (T[l] == 'Q'){
              T[l] = '0' + j;
            }
            if (T[l] == 'R'){
              T[l] = '0'+ k;
            }
          }
          int pos = 0;
          if (parse(T, pos) == 2){
            ans++;
          }
        }
      }
    }
    cout << ans << endl;
  }
}
