#include <bits/stdc++.h>
using namespace std;

const int maxN = 1e6 + 10;

int n, m;
int sum[maxN + 1], mx[maxN + 1];
char s[maxN + 1];

inline int solve(int rule)
{
	int res = 0, mn = 0;
	for(int i = 1; i <= n; i++)
	{
		if(s[i] == '?' && mx[i] + res + 2 <= rule) res += 2;
		mn = min(mn, sum[i] + res);
	}
	return mn;
}

int main()
{
	scanf("%s", s + 1);
	n = strlen(s + 1);

	for(int i = 1; i <= n; i++)
		if(s[i] == '1') sum[i] = sum[i - 1] + 1;
		else sum[i] = sum[i - 1] - 1;
	mx[n] = sum[n];
	for(int i = n - 1; i >= 1; i--) mx[i] = max(mx[i + 1], sum[i]);
	m = max(0, mx[1]);

	printf("%d\n", min(m - solve(m), m + 1 - solve(m + 1)));
	return 0;
}
