#include<bits/stdc++.h>
#define llong long long
#define mkpr make_pair
#define x first
#define y second
#define iter iterator
#define riter reversed_iterator
#define y1 Lorem_ipsum_
#define tm dolor_sit_amet_
using namespace std;

inline int read()
{
	int x = 0,f = 1; char ch = getchar();
	for(;!isdigit(ch);ch=getchar()) {if(ch=='-') f = -1;}
	for(; isdigit(ch);ch=getchar()) {x = x*10+ch-48;}
	return x*f;
}

const int mxN = 1e6;
char a[mxN+3];
int b[mxN+3];
int s[mxN+3];
int mn[mxN+3];
int n;

int main()
{
	scanf("%s",a+1); n = strlen(a+1);
	int cur = 0,cur2 = 0,lb = 0;
	for(int i=1; i<=n; i++) {b[i] = (a[i]=='1'?-1:1); s[i] = s[i-1]+b[i]; lb = min(lb,s[i]);}
	mn[n] = s[n]; for(int i=n-1; i>=0; i--) {mn[i] = min(s[i],mn[i+1]);}
	int ans = 0;
	for(int i=1; i<=n; i++)
	{
		if(a[i]=='?')
		{
			if(mn[i]-cur*2-2>=lb) {cur++; b[i] = -1;}
		}
	}
	for(int i=1; i<=n; i++) {s[i] = s[i-1]+b[i]; ans = max(ans,s[i]);}
	ans -= lb;
	
	for(int i=1; i<=n; i++) {b[i] = (a[i]=='1'?-1:1); s[i] = s[i-1]+b[i];}
	mn[n] = s[n]; for(int i=n-1; i>=0; i--) {mn[i] = min(s[i],mn[i+1]);}
	int ans2 = 0; lb--; cur = 0;
	for(int i=1; i<=n; i++)
	{
		if(a[i]=='?')
		{
			if(mn[i]-cur*2-2>=lb) {cur++; b[i] = -1;}
		}
	}
	for(int i=1; i<=n; i++) {s[i] = s[i-1]+b[i]; ans2 = max(ans2,s[i]);}
	ans2 -= lb;
	printf("%d\n",min(ans,ans2));
	return 0;
}
//	printf("ans=%d\n",ans);
//	printf("b: "); for(int i=1; i<=n; i++) printf("%d ",b[i]); puts("");
//	printf("s: "); for(int i=1; i<=n; i++) printf("%d ",s[i]); puts("");