#include <bits/stdc++.h>
using namespace std;
int par[4'000'005];
int type[4'000'005];
vector<int> adj[4'000'005];
int sz[4'000'005];
pair<int,int> ans[4'000'005];
void dfs(int n, int p) {
if (type[n]==0) sz[n]=1;
else sz[n]=0;
vector<int> tmp;
for (auto i:adj[n]) if (i!=p) {
dfs(i,n);
tmp.push_back(i);
sz[n]+=sz[i];
}
if (type[n]==0) ans[n]={1,1};
else if (type[n]==-1){
//the idea is, if you can get {s,e} from 1 to sz, {s2, e2} from 1 to sz2,
//if you take min gate you can get from min(s,s2) to e+e2. or something liddat.
ans[n]={min(ans[tmp[0]].first, ans[tmp[1]].first),ans[tmp[0]].second+ans[tmp[1]].second-1};
}
else {
ans[n]={ans[tmp[0]].first+ans[tmp[1]].first,max(sz[tmp[0]]+ans[tmp[1]].second, sz[tmp[1]]+ans[tmp[0]].second)};
}
}
int main() {
string s; cin >> s;
//you are supposed to build a stupid binary tree from this string.
int cur=0;
int idx=0;
par[0]=-1;
for (int i = 0; i < s.size(); i++) {
if (s[i]=='i') {
//create a new node here
idx++;
par[idx]=cur;
type[idx]=-1;
//move to child
cur=idx;
}
else if (s[i]=='a') {
idx++;
par[idx]=cur;
type[idx]=1;
cur=idx;
}
else if (s[i]=='?') {
idx++;
par[idx]=cur;
}
else if (s[i]==')') cur=par[cur];
}
for (int i = 1; i <= idx; i++) {
adj[i].push_back(par[i]);
adj[par[i]].push_back(i);
}
dfs(0,-1);
cout << ans[1].second-ans[1].first+1;
}