#include<bits/stdc++.h>
using namespace std;
int n;
char s[1010101];
int a[1010101];
int b[1010101];
int main()
{
	scanf("%s", s + 1);
	n = strlen(s + 1);
	for(int i = 1; i <= n; i++)
	{
		if(s[i] == '1')
			a[i] = 1;
		else
			a[i] = -1;
		a[i] += a[i-1];
	}
	b[n] = a[n];
	for(int i = n - 1; i >= 0; i--)
		b[i] = max(b[i+1], a[i]);
	
	int c = 0, mi = 0;
	for(int i = 1; i <= n; i++)
	{
		if(s[i] == '?' && c + 2 + b[i] <= b[0])
			c += 2;
		mi = min(mi, a[i] + c); 
	}
	int ans = 1e7;
	ans = min(ans, b[0] - mi);
	c = 0, mi = 0;
	for(int i = 1; i <= n; i++)
	{
		if(s[i] == '?' && c + 2 + b[i] <= b[0] + 1)
			c += 2;
		mi = min(mi, a[i] + c); 
	}
	ans = min(ans, b[0] + 1 - mi);
	printf("%d\n", ans);
	return 0;
}