Under construction

Still being built. Things may change

Function String

Interview

Time limit1sMemory limit1024 MB

Summary
Evaluate a prefix-notation string of f, g, and x, where x is 0, g adds 1, and f takes the smaller of two sub-strings, or print -1 if the string is malformed.
Level

Easy3 of 10

Topics
String, Stack
Solved
No attempts yet

Problem

A function string that evaluates to a single integer is defined as follows.

  1. x is a function string. The string above evaluates to 00.
  2. If S is a function string, then gS is also a function string. The string above evaluates to the value of S plus 11.
  3. If S and T are function strings, then fST is also a function string. The string above evaluates to the smaller of the values that S and T evaluate to.

Input

The first line contains a string SS consisting only of f, g, and x. (1≤∣S∣≤1 000 000)(1 \leq |S| \leq 1\,000\,000)

Output

On the first line, print the value of the string if it is a function string. Otherwise, print -1.

Examples3

  1. Example 1

    Input
    fggxgx
    
    Expected output
    1
    
  2. Example 2

    Input
    xxx
    
    Expected output
    -1
    
  3. Example 3

    Input
    gggx
    
    Expected output
    3