#include <bits/stdc++.h>
using namespace std;
#define int long long
#define f first
#define s second
#define pb push_back
#define sz(x) (int)x.size()
const int maxn=2000005;
int p[maxn];
int par(int x){
if(p[x]==0)return x;
return p[x]=par(p[x]);
}
signed main(){
string s;cin>>s;
vector<char> t(maxn);
vector<vector<int>> ch(maxn), cl(maxn);
vector<int> v, pa(maxn);
int ptr=1, n=0;
for(char c : s){
if(c=='m' or c=='a' or c=='i' or c=='(' or c==')' or c==',')continue;
int ind = ptr++;
t[ind]=c;
if(c=='?'){
n++;
assert(t[v.back()] == 'n' or t[v.back()] == 'x');
int cur=ind;
while(!v.empty()){
ch[v.back()].pb(cur);
pa[cur]=v.back();
if(t[v.back()] == t[cur])p[cur]=v.back();
if(sz(ch[v.back()]) == 2){
cur=v.back();
v.pop_back();
}
else break;
}
}
else v.pb(ind);
/*for(auto it : v){
cout<<t[it]<<" "<<sz(ch[it])<<", " ;
}
cout<<endl;*/
}
for(int i=1;i<ptr;i++){
int head=par(i);
for(auto it : ch[i]){
if(t[it] != t[i]){
cl[head].pb(it);
}
}
}
vector<int> ss(maxn, 0);
vector<pair<int,int>> r(maxn, {-1, -1});
auto dfs=[&](auto && dfs, int x) -> void{
int nc=0;
for(auto it : cl[x]){
nc++;
//cout<<"children " << it<<endl;
}
for(auto it : cl[x]){
dfs(dfs, it);
if(r[x].f == -1) r[x]=r[it];
else {
if(t[x] == 'x'){
r[x] = {r[x].f + r[it].f, max(r[x].s + ss[it], r[it].s + ss[x])};
}
else {
r[x] = {min(r[x].f, r[it].f), r[x].s + r[it].s - 1};
}
}
ss[x] += ss[it];
}
if(nc==0){
ss[x]++;
r[x]={1,1};
}
/*cout<<"at node x " << x<<" type is " << t[x]<<endl;
printf("ss %lld, a %lld, b %lld\n", ss[x], r[x].f, r[x].s);*/
};
dfs(dfs, 1);
cout<<r[1].s - r[1].f + 1;
}