/*
Though leaves are many , the root is one.
Through all the lying days of my youth
I swayed my leaves and flowers in the sun.
Now I may wither into the truth.
	  	  	- William Butler Yeats
*/
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
//#pragma GCC target("avx,tune=native")
#include<bits/stdc++.h>
#pragma comment("-Wl,--stack=1024000000")
//#include<ext/pb_ds/assoc_container.hpp>
using namespace std;
//using namespace __gnu_pbds;
const int inf=0x3f3f3f3f;
const double eps=1e-6;
const int mod=1000000007;
typedef long long ll;
#ifndef LOCAL
#define cerr if(0)cout
#define eprintf(...) 0
#else
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#endif
inline string getstr(string &s,int l,int r){string ret="";for(int i=l;i<=r;i++)ret.push_back(s[i]);return ret;}
int modpow(int x,int y,int md=mod){int ret=1;do{if(y&1)ret=(ll)ret*x%md;x=(ll)x*x%md;}while(y>>=1);return ret;}
inline int Rand(){return rand()*32768+rand();}
string s;
int suf[1000005],pref[1000005];
int solve(int x){
	string ss=s;
	int add=0,sum=0;
	for(int i=0;i<s.size();i++){
		if(s[i]!='?'){
			sum+=(s[i]=='1'?1:-1);
			continue;
		}
		if(sum+add+suf[i]<=x-2){
			add+=2;s[i]='1';
		}else s[i]='0';
		sum-=1;
	}
	pref[0]=s[0]=='1'?1:-1;
	for(int i=1;i<s.size();i++)pref[i]=pref[i-1]+(s[i]=='1'?1:-1);
	int mx=max(0,*max_element(pref,pref+s.size())),mn=min(0,*min_element(pref,pref+s.size()));
	s=ss;
	if(mx!=x)while(1);
	return mx-mn;
}
int main(){
	cin>>s;
	int f=0,g=0; 
	for(char c:s)f+=(c=='1'?1:-1),g=max(g,f);
	int cur=0;
	for(int i=s.size()-1;i>=0;i--){
		cur+=(s[i]=='1'?1:-1);
		if(cur<0)cur=0;
		suf[i]=cur;
	}
	cout<<min(solve(g),solve(g+1))<<endl;
	return 0;
}