import java.util.Scanner;

/**
 * Parser for simple expressions.
 * 
 * @author Paul Chew
 * 
 * Created for CS211, Sept 2005.
 */
public class SimpleExpression {
    
    /**
     * Main program.
     */
    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);
        String line = sc.nextLine();
        while (line.length() != 0) {
            System.out.println(line);
            Scanner scanner = new Scanner(line);
            boolean valid = parseE(scanner);
            if (valid && scanner.hasNext()) {
                valid = false;
                System.err.println("Extra token: " + scanner.next());
            }
            System.out.println(valid? "Valid" : "Invalid");
            line = sc.nextLine();
        }
        System.out.println("Exiting");
    }
    
    /**
     * Check that scanner's next token matches the given string.
     * @param scanner the scanner
     * @param string the String to look for
     * @return true iff the string appears as the next token
     */
    public static boolean check (Scanner scanner, String string) {
        if (!scanner.hasNext()) {
            System.err.println("Missing token: " + string);
            return false;
        }
        String token = scanner.next();
        if (!token.equals(string)) {
            System.err.println("Expected " + string + ", but found " + token);
            return false;
        }
        return true;
    }
    
    /**
     * Parse E, a simple expression.
     * @param scanner the scanner
     * @return true iff scanner holds a valid expression
     */
    public static boolean parseE (Scanner scanner) {
        if (scanner.hasNextInt()) {
            scanner.nextInt();
            return true;
        }
        return check(scanner, "(") &&
            parseE(scanner) &&
            check(scanner, "+") &&
            parseE(scanner) &&
            check(scanner, ")");
    }
}