#include<cstdio>
#include<cstring>
#include<iostream>
#define RI register int
#define CI const int&
using namespace std;
const int N=1000005;
char s[N]; int n,pfx[N],smi[N];
inline int work(CI mi)
{
	RI i; int cur=0,mx=0; for (i=1;i<=n;++i)
	{
		if (s[i]=='?'&&smi[i]-cur-2>=mi) cur+=2;
		mx=max(mx,pfx[i]-cur);
	}
	return mx-mi;
}
int main()
{
	RI i; for (scanf("%s",s+1),n=strlen(s+1),i=1;i<=n;++i)
	pfx[i]=pfx[i-1]+(s[i]=='0'?-1:1); smi[n]=pfx[n];
	for (i=n-1;~i;--i) smi[i]=min(smi[i+1],pfx[i]);
	return printf("%d",min(work(smi[0]),work(smi[0]-1))),0;
}