#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define ll long long
#define maxn 1000005
#define inf 1e9
#define pb push_back
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
using namespace std;

inline int read()
{
	int x=0,w=1; char c=getchar();
	while(c<'0'||c>'9') {if(c=='-') w=-1; c=getchar();}
	while(c<='9'&&c>='0') {x=(x<<1)+(x<<3)+c-'0'; c=getchar();}
	return w==1?x:-x;
}

char s[maxn];
int len,suf[maxn],pre[maxn],mn,ans;

int main()
{
	scanf("%s",s+1); len=strlen(s+1); mn=0; ans=inf;
	rep(i,1,len) pre[i]=pre[i-1]+(s[i]=='0'?-1:1),mn=min(mn,pre[i]);
	suf[len]=pre[len]; per(i,len-1,1) suf[i]=min(suf[i+1],pre[i]);
	rep(j,1,2)
	{
		int mx=0,nw=0,p=0;
		rep(i,1,len)
		{
			if(s[i]!='?') nw+=(s[i]=='0'?-1:1);
			else
			{
				if(suf[i]-pre[i-1]+nw-2>=mn) nw--;
				else nw++;
			}
			p=max(p,nw);
		}
		ans=min(ans,p-mn); mn--;
	}
	cout<<ans<<endl;
	return 0;
}