import std.stdio, std.string, std.conv, std.array;

string inv( string v ) {
  return to!string(2 - to!int(v));
}

string mult( string x, string y ) {
  if ( x == "0" || y == "0" ) return "0";
  else if ( x == "1" || y == "1" ) return "1";
  else return "2";
}

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

void main()
{
  while ( true ) {
	string input = strip( readln );
	if ( input == "." ) break;
	int count = 0;
	foreach ( p; [ "0", "1", "2" ] ) {
	  foreach ( q; [ "0", "1", "2" ] ) {
		foreach ( r; [ "0", "1", "2" ] ) {
		  auto exp =  replace( replace( replace( input, "P", p ), "Q", q ), "R", r );
		  Tokenizer tk = new Tokenizer( exp );
		  // stderr.writeln( exp );
		  if ( apply( tk ) == "2" ) {
			count++;
		  }
		}
	  }
	}
	writeln( count );
  }
}

class Tokenizer {
  string str;
  int index;
  this( string str ) {
	this.str = str;
	this.index = 0;
  }
  string nextToken() {
	auto ans = str[ index..index+1 ];
	index++;
	return ans;
  }
  bool eof() {
	return index == str.length;
  }
}

string apply( Tokenizer tk ) {
  if ( !tk.eof ) {
	auto nt = tk.nextToken;
	switch( nt ) {
	case "0":
	case "1":
	case "2":
	  return nt;
	case "-":
	  auto token = apply( tk );
	  return inv( token );
	case "(":
	  auto token1 = apply( tk );
	  auto op = tk.nextToken();
	  auto token2 = apply( tk );
	  tk.nextToken(); // )
	  if ( op == "+" ) {
		return add( token1, token2 );
	  } else if ( op == "*" ) {
		return mult( token1, token2 );
	  } else {
		return "";
	  }
	default:
	  return "";
	}
  } else {
	return "";
  }
}