#include <iostream>
#include <algorithm>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <string.h>
#include <stack>
#include <assert.h>
#include <bitset>
#define Endl endl
#define mp make_pair
#define ll long long 
#define ull unsigned long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define over(A) {cout<<A<<endl;exit(0);}
#define all(A) A.begin(),A.end()
#define ceil(a,b) ((a-1)/b+1)
#define quickcin ios_base::sync_with_stdio(false);
const int mod=1e9+7;
const int inf=1039074182;
const ll llinf=1LL*inf*inf;
using namespace std;
template <typename T1,typename T2> inline void chmin(T1 &x,T2 b) {if(b<x) x=b;}
template <typename T1,typename T2> inline void chmax(T1 &x,T2 b) {if(b>x) x=b;}
template <typename T1,typename T2> inline void chadd(T1 &x,T2 b) {x+=b;if(x>=mod) x-=mod;}
template <typename T1,typename T2> inline void chmul(T1 &x,T2 b) {x=1LL*x*b%mod;}
template <typename T1,typename T2> inline void chmod(T1 &x,T2 b) {x%=b,x+=b;if(x>=b) x-=b;}
string s;
int n;
int sum[1000005];
int maxch[1000005];

int main()
{
//	freopen("input.txt","r",stdin);
	cin>>s;
	n=s.size();
	string ts=s;
	int mx=0;
	for(int i=0;i<n;i++)
	{
		sum[i]=(i?sum[i-1]:0)+(s[i]=='1'?1:-1);
		chmax(mx,sum[i]);
	}
	int res=inf;
	for(int k=0;k<2;k++)
	{
		s=ts;
		int mx=0;
		for(int i=0;i<n;i++)
		{
			sum[i]=(i?sum[i-1]:0)+(s[i]=='1'?1:-1);
			chmax(mx,sum[i]);
		}
		mx+=k;
		for(int i=n-1;i>=0;i--)
		{
			maxch[i]=min((i==n-1?inf:maxch[i+1]),(mx-sum[i])/2);
		}
		int ss=0;
		for(int i=0;i<n;i++)
		{
			if(s[i]!='?') continue;
			if(ss<maxch[i])
			{
				ss++;
				s[i]='1';
			}
		}
		for(int i=0;i<n;i++)
		{
			if(s[i]=='?') s[i]='0';
		}
		int mn=0;
		for(int i=0;i<n;i++)
		{
			sum[i]=(i?sum[i-1]:0)+(s[i]=='1'?1:-1);
			chmin(mn,sum[i]);
		}
		chmin(res,mx-mn);
	}
	cout<<res<<endl;
	return 0;
}