#include <bits/stdc++.h>

using namespace std;

const int MAXN = 1000005;

int n;

char s[MAXN];

bool ok(int lim)
{
	bool flag[2] = {1,1};
	for (int par = 0;par <= 1;par++)
	{
		int l = par,r = ((lim & 1) == par ? lim : lim - 1);
		if (l > r)
			flag[par] = 0;
		for (int i = 1;i <= n;i++)
		{
			if (s[i] == '?')
			{
				l--;
				r++;
			}
			else
			{
				l += (s[i] == '0' ? -1 : 1);
				r += (s[i] == '0' ? -1 : 1);
			}
			if (l < 0)
				l += 2;
			if (r > lim)
				r -= 2;
			if (l > r)
				flag[par] = 0;
		}
	}
	return flag[0] || flag[1];
}

int main()
{
	scanf("%s",s + 1);
	n = strlen(s + 1);
	int low = 1,high = n;
	while (low < high)
	{
		int mid = low + high >> 1;
		if (ok(mid))
			high = mid;
		else
			low = mid + 1;
	}
	printf("%d\n",low);
	return 0;
}