//
// Created by Bianca Grava on 21/11/2026.
//
#include<iostream>
#include<fstream>
#include<stack>
#include<string>
#include<vector>
using namespace std;
ifstream in("evaluare.in");
ofstream out("evaluare.out");
int precedence(char ch) {
if (ch=='^') return 3;
if (ch=='*'||ch=='/') return 2;
if (ch=='+'||ch=='-') return 1;
}
vector<string> postfixata(const string &infi) {
string operand="";
vector<string> result;
stack<char> s;
for (char ch : infi) {
if (ch>='0'&&ch<='9')
operand+=ch;
else {
if (ch=='(') {
s.push('(');
}else {
if (!operand.empty()) {
result.push_back(operand);
operand="";
}
if (ch==')') {
while (!s.empty()&&s.top()!='(') {
result.push_back(string(1,s.top()));
s.pop();
}
if (!s.empty())
s.pop();
}else {
//ch is an operator
if (!operand.empty()) {
result.push_back(operand);
operand="";
}
while (!s.empty()&&precedence(ch)<=precedence(s.top())&&s.top()!='(') {
result.push_back(string(1,s.top()));
s.pop();
}
s.push(ch);
}
}
}
}
result.push_back(operand);
while (!s.empty()) {
result.push_back(string(1,s.top()));
s.pop();
}
return result;
}
int evaluare(const vector<string> &postfi) {
stack<string> st;
int op1,op2;
for (string s: postfi) {
if (s[0]>='0'&&s[0]<='9')
st.push(s);
else {
if (st.empty())
return -1;
op1=stoi(st.top());
st.pop();
if (st.empty())
return -1;
op2=stoi(st.top());
st.pop();
if (s=="-")
op1=op2-op1;
else {
if (s=="+")
op1=op2+op1;
else {
if (s=="*")
op1=op2*op1;
else {
if (s=="/")
op1=op2/op1;
else {
if (s=="^")
op1=op2^op1;
else
return -1;
}
}
}
}
st.push(to_string(op1));
}
}
return stoi(st.top());
}
int main(){
string s;
in>>s;
vector<string> post=postfixata(s);
out<<evaluare(post);
//if (result!=-1)
// out<<result;
//else
//out<<"incorect"<<endl;
return 0;
}