#include <iostream>
#include<set>
#include<vector>
#include<string>
using namespace std;
const int mod=1000000007;
int add(int a, int b)
{
	int c=a+b;
	if(c>=mod)c-=mod;
	return c;
}
int dif(int a, int b)
{
	int c=a-b;
	if(c<0)c+=mod;
	return c;
}
int mlt(int a, int b)
{
	long long c=a*1LL*b;
	return c%mod;
}
int bp(int a, int b)
{
	if(b==0)return 1;
	if(b%2==0)
	{
		int val=bp(a, b/2);
		return mlt(val, val);
	}
	if(b%2==1)
	{
		int val=bp(a, b-1);
		return mlt(val, a);
	}
}
int inv(int a)
{
	return bp(a, mod-2);
}
void outp(vector<vector<int>>&v)
{
	for(int i=0; i<v.size(); i++)
	{
		for(int j=0; j<v[i].size(); j++)cout<<v[i][j]<<' ';
		cout<<endl;
	}
	cout<<endl;
}
bool tst(string s, int k, int a, int b)
{
	int cura=a, curb=b;
	for(int j=0; j<s.length(); j++)
	{
		if(s[j]=='0')
		{
			if(curb==k)
				curb--;
			else curb++;
			cura++;
			if(cura>curb)return false;
			if(cura<0)return false;
		}
		if(s[j]=='1')
		{
			curb--;
			cura--;
			if(cura<0)cura+=2;
			if(cura>curb)return false;
		}
		if(s[j]=='?')
		{
			cura--;
			if(cura<0)cura=1;
			curb++;
			if(curb>k)curb-=2;
			if(cura>curb)return false;
		}
	}
	return true;
}
bool check(string s, int k)
{
	if(k%2==0)
	{
		if(tst(s, k, 0, k))return true;
		if(tst(s, k, 1, k-1))return true;
		return false;
	}
	else
	{
		if(tst(s, k, 0, k-1))return true;
		if(tst(s, k, 1, k))return true;
		return false;
	}
}
int main()
{
	string s;
	cin>>s;
	int n=s.length();
	vector<int>l0(n+1), l1(n+1);
	int maxval=0;
	for(int i=1; i<=n; i++)
	{
		if(s[i-1]=='0')
		{
			l0[i]=l0[i-1]+1;
		}
		else{
			l0[i]=max(l0[i-1]-1, 0);
		}
		if(s[i-1]=='1')
		{
			l1[i]=l1[i-1]+1;
		}
		else{
			l1[i]=max(l1[i-1]-1, 0);
		}
		maxval=max(maxval, l0[i]);
		maxval=max(maxval, l1[i]);
	}
	if(check(s, maxval))cout<<max(maxval,1)<<endl;
	else cout<<maxval+1<<endl;
}