#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>

#define MAX 100

char stack[MAX][MAX];
int top = -1;

void push(char *str){
    strcpy(stack[++top], str);
}

char* pop(){
    return stack[top--];
}

int isOperator(char ch){
    return ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '^';
}

int isValidChar(char ch){
    return isOperator(ch) || isalnum(ch) || ch == ' ' || ch == '(' || ch == ')';
}

int isBalance(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(isValidChar(expr[i])){
            printf("Invalid input\n");
            return 0;
        }
    }
    
    if(!isBalance(expr)){
        printf("Invalid input");
        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(isOpertor(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());
            sprintf(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;
}