#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10;
char s[maxn];
int n;
int L[maxn], R[maxn], par[maxn];
bool check(int mid)
{
	for(int i = 0; i <= n; ++i) L[i] = R[i] = par[i] = 0;
	L[n] = 0; R[n] = mid;
	for(int i = n - 1; ~i; --i)
	{
		if(s[i + 1] == '0')
		{
			L[i] = L[i + 1] + 1;
			R[i] = min(mid, R[i + 1] + 1);
			if(par[i + 1] && R[i + 1] == mid) R[i] = min(R[i], mid - 1);
		}
		else if(s[i + 1] == '1')
		{
			L[i] = max(0, L[i + 1] - 1);
			R[i] = R[i + 1] - 1;
			if(par[i + 1] && L[i + 1] == 0) L[i] = max(L[i], 1);
		}
		else
		{
			L[i] = max(0, L[i + 1] - 1);
			R[i] = min(mid, R[i + 1] + 1);
			if(par[i + 1])
			{
				if(L[i + 1] == 0) L[i] = max(L[i], 1);
				if(R[i + 1] == mid) R[i] = min(R[i], mid - 1);
			}
		}
		if(L[i] > R[i]) return 0;
		if(L[i] == R[i] || par[i + 1]) par[i] = 1;
	}
	return 1;
}
int main()
{
	scanf("%s", s + 1);
	n = strlen(s + 1);
	int l = 1, r = n;
	while(l <= r)
	{
		int mid = l + r >> 1;
		if(check(mid)) r = mid - 1;
		else l = mid + 1;
	}
	printf("%d\n", l);
	return 0;
}