#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define max 300

char stack[max][max];
int top = -1;

void push(char *str){
    strcpy(stack[++top], str, max-1);
    stack[top][max-1] = '\0';
}

char* pop(){
    return stack[top--];
}

int isop(char ch){
    return ch == '+' || ch == '-' || ch == '*' ||ch == '/' || ch == '^';
}

int isval(char ch){
    return isop(ch) || isalnum(ch) || ch == ' ' || ch == '(' || ch == ')';
}

int isbal(char *expr){
    int count = 0;
    for(int i=0; expr[i];i++){
        if (expr[i] == '(') count++;
        else if (expr[i] == ')' )count--;
        if(count < 0) return 0;
    }
    return count == 0;
}

int main(){
    char expr[max];
    fgets(expr, max, stdin);
    
    for (int i=0; expr[i]; i++){
        if(!isval(expr[i])){
            printf("Invalid input\n");
            return 0;
        }
    }
    
    if(!isbal(expr)){
        printf("Invalid input\n");
        return 0;
    }
    
    char *token = strtok(expr, " \n");
    char *rev[max];
    int count = 0;
    
    while(token != NULL){
        rev[count++]=token;
        token = strtok(NULL, " \n");
    }
    
    for(int i=count - 1; i>= 0; i--){
        if(isop(rev[i][0]) && strlen(rev[i]) == 1){
            if(top <1){
                printf("Invalid input\n");
                return 0;
            }
            char op1[max], op2[max], temp[max];
            strcpy(op1, pop());
            strcpy(op2, pop());
            snprintf(temp, sizeof(temp), "%s%s%s", op1, op2, rev[i]);
            push(temp);
        }else{
            push(rev[i]);
        }
    }
    if(top!=0){
        printf("Invalid input\n");
    }else{
        printf("%s\n", pop());
    }
    return 0;
}