#include <bits/stdc++.h>
using namespace std;
class ParseError {};
int expression(string::const_iterator &begin);
int number(string::const_iterator &begin);
int factor(string::const_iterator &begin);
int denial(string::const_iterator &begin);
void consume(string::const_iterator &begin, char expected);
int p, q, r;
int expression(string::const_iterator &begin) {
  int ret = factor(begin);
  while (true) {
    if (*begin == '+') {
      consume(begin, '+');
      int num = factor(begin);
      if (ret == 0 && num == 0) {
        ret = 0;
      } else if (ret == 1 || num == 1) {
        if (ret != 2 && num != 2) {
          ret = 1;
        } else {
          ret = 2;
        }
      } else {
        ret = 2;
      }
    } else if (*begin == '*') {
      consume(begin, '*');
      int num = factor(begin);
      if (num == 2 && ret == 2) {
        ret = 2;
      } else if (num == 0 || ret == 0) {
        ret = 0;
      } else {
        ret = 1;
      }
    } else {
      break;
    }
  }
  return ret;
}
int denial(string::const_iterator &begin) {
  if (*begin == '-') {
    consume(begin, '-');
    int ret = denial(begin);
    if (ret == 2) {
      ret = 0;
    } else if (ret == 0) {
      ret = 2;
    }
    return ret;
  }
  return number(begin);
}
int number(string::const_iterator &begin) {
  if (*begin == 'P') {
    consume(begin, 'P');
    return p;
  } else if (*begin == 'Q') {
    consume(begin, 'Q');
    return q;
  } else if (*begin == 'R') {
    consume(begin, 'R');
    return r;
  }
  int ret = 0;
  while (isdigit(*begin)) {
    ret *= 10;
    ret += *begin - '0';
    begin++;
  }
  return ret;
}
int factor(string::const_iterator &begin) {
  if (*begin == '(') {
    consume(begin, '(');
    int ret = expression(begin);
    consume(begin, ')');
    return ret;
  } else {
    return denial(begin);
  }
}
void consume(string::const_iterator &begin, char expected) {
  if (*begin == expected) {
    begin++;
  } else {
    cerr << "Expected '" << expected << "' but got '" << *begin << "'" << endl;
    cerr << "Rest string is '";
    while (*begin) {
      cerr << *begin++;
    }
    cerr << "'" << endl;
    throw ParseError();
  }
}
string input;
int main() {
  while (true) {
    cin >> input;
    if (input[0] == '.') {
      break;
    }
    int ans = 0;
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        for (int k = 0; k < 3; k++) {
          p = i;
          q = j;
          r = k;
          string::const_iterator s = input.begin();
          if (factor(s) == 2) {
            ans++;
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
