#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1000005,p=998244353,inf=0x3f3f3f3f;
int read(){
	int f=1,g=0;
	char ch=getchar();
	for (;!isdigit(ch);ch=getchar()) if (ch=='-') f=-1;
	for (;isdigit(ch);ch=getchar()) g=g*10+ch-'0';
	return f*g;
}
int n,g[N],ans;
char ch[N];
int work(int R){
	int ta=0,tb=0,cur=0;
	for (int i=1;i<=n;i++){
		if (ch[i]!='?') cur+=(ch[i]=='1') ? 1 : -1;
		else cur+=(cur+1+g[i+1]<=R) ? 1 : -1;
		ta=max(ta,cur);tb=min(tb,cur);
	}
	return ta-tb;
}
int main(){
//	freopen("a.txt","r",stdin);
	scanf("%s",ch+1);
	n=strlen(ch+1);
	for (int i=n;i;i--)	g[i]=max(0,g[i+1]+((ch[i]=='1') ? 1 : -1));
	ans=min(work(g[1]),work(g[1]+1));
	printf("%d\n",ans);
	return 0;
}