#include<stdio.h>
#include<string.h>
#include<ctype.h>
#define MAX 1000
int isOperator(char c) {
    return (c == '+' || c =='-' || c == '*' || c == '\/' || c == '^');
}

int isOperandChar(char c) {
    return (isalnum((unsigned char)c));
}

int isValidChar(char c) {
    return (isOperandChar(c) || isOperator(c) || c == '(' || c == ')' || isspace((unsigned char)c));
}

int main() {
    char input[MAX];
    if (!fgets(input, sizeof(input), stdin)) {
        printf("Invalid input\n");
        return 0;
    }
    int len = strlen(input);
    int balance = 0;
    char tokens[MAX][MAX];
    int tcount = 0;
    
    for (int i = 0; i < len; ) {
        char c = input[i];
        
        if (!isValidChar(c)) {
            printf("Invalid input\n");
            return 0;
        }
        
        if (isspace((unsigned char)c)) {
            i++;
            continue;
        }
        
        if ( c == '(') {
            balance++;
            tokens[tcount][0] = c;
            tokens[tcount][1] = '\0';
            tcount++;
            i++;
        }else if ( c == ')') {
            balance--;
            if (balance < 0) {
                printf("Invalid input\n");
                return 0;
            }
            tokens[tcount][0] = c;
            tokens[tcount][1] = '\0';
            tcount++;
            i++;
        } else if (isOperator(c)) {
           tokens[tcount][0] = c;
            tokens[tcount][1] = '\0';
            tcount++;
            i++; 
        } else if (isOperandChar(c)) {
            int j = 0;
            while (i < len && isOperandChar(input[i])) {
                tokens[tcount] [j++] = input[i++];
            }
            tokens[tcount][j] = '\0';
            tcount++;
        } else {
            printf("Invalid input\n");
            return 0;
        }
    }
    
    if (balance != 0) {
        printf("Invalid input\n");
        return 0;
    }
    
    if (tcount == 0) {
        printf("Invalid input\n");
        return 0;
    }
    
    char clean[MAX][MAX];
    int ccount = 0;
    for ( int i =0; i < tcount; i++) {
          if (tokens[i][0] == '(' && tokens[i][1] == '\0') continue;
           if (tokens[i][0] == ')' && tokens[i][1] == '\0') continue;
           strcpy(clean[ccount++], tokens[i]);
    }
    if (ccount == 0) {
        printf("Invalid input\n");
        return 0;
    }
    
    char stack[MAX][MAX];
    int top = -1;
    int operandCount = 0;
    
    for (int  i = ccount - 1; i >= 0; i--) {
        char *tok = clean[i];
        
        if (strlen(tok) == 1 && isOperator(tok[0])) {
            
            if (top < 1) {
                printf("Invalid input\n");
                return 0;
            }
            char op1[MAX], op2[MAX], res[MAX * 2];
            strcpy(op1, stack[top--]);
             strcpy(op2, stack[top--]);
            
            snprintf(res, sizeof(res), "%s%s%c", op1, op2, tok[0]);
             strcpy(stack[++top], res);
             operandCount--;
        } else {
            strcpy(stack[++top], tok);
            operandCount++;
        }
    }
    if (top != 0 || operandCount != 1) {
        printf("Invalid input\n");
        return 0;
    }
    printf("%s\n", stack[0]);
    return 0;
}