#include<bits/stdc++.h>
using namespace std;

#define int long long

#define rep(i,n) for(int i=0;i<(n);i++)
#define pb push_back
#define eb emplace_back
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second

using vint=vector<int>;
using pint=pair<int,int>;
using vpint=vector<pint>;

template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}

template<class A,class B>
ostream& operator<<(ostream& ost,const pair<A,B>&p){
	ost<<"{"<<p.first<<","<<p.second<<"}";
	return ost;
}

template<class T>
ostream& operator<<(ostream& ost,const vector<T>&v){
	ost<<"{";
	for(int i=0;i<v.size();i++){
		if(i)ost<<",";
		ost<<v[i];
	}
	ost<<"}";
	return ost;
}

inline int topbit(unsigned long long x){
	return x?63-__builtin_clzll(x):-1;
}

inline int popcount(unsigned long long x){
	return __builtin_popcountll(x);
}

inline int parity(unsigned long long x){
	return __builtin_parity(x);
}

int N;
string S;

int calc(int M){
	vint A(N+1);
	rep(i,N){
		A[i+1]=A[i];
		if(S[i]=='1')A[i+1]++;
		else A[i+1]--;
	}
	vint suf=A;
	for(int i=N-1;i>=0;i--)chmax(suf[i],suf[i+1]);

	int offset=0;
	int mi=0;
	rep(i,N){
		if(S[i]=='?'&&suf[i+1]+offset+2<=M)offset+=2;
		chmin(mi,A[i+1]+offset);
	}
	return M-mi;
}

signed main(){
	cin>>S;
	N=S.size();

	int cur=0;
	int m=0;
	rep(i,N){
		if(S[i]=='1')cur++;
		else cur--;
		chmax(m,cur);
	}

	cout<<min(calc(m),calc(m+1))<<endl;
	return 0;
}