#include <string>
#include <vector>
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<stack>
#include<queue>
#include<cmath>
#include<algorithm>
#include<functional>
#include<list>
#include<deque>
#include<bitset>
#include<set>
#include<map>
#include<unordered_map>
#include<unordered_set>
#include<cstring>
#include<sstream>
#include<complex>
#include<iomanip>
#include<numeric>
#include<cassert>
#include<random>
#define X first
#define Y second
#define pb push_back
#define rep(X,Y) for (int (X) = 0;(X) < (int)(Y);++(X))
#define reps(X,S,Y) for (int (X) = (int)(S);(X) < (int)(Y);++(X))
#define rrep(X,Y) for (int (X) = (int)(Y)-1;(X) >=0;--(X))
#define rreps(X,S,Y) for (int (X) = (int)(Y)-1;(X) >= (int)(S);--(X))
#define repe(X,Y) for ((X) = 0;(X) < (Y);++(X))
#define peat(X,Y) for (;(X) < (Y);++(X))
#define all(X) (X).begin(),(X).end()
#define rall(X) (X).rbegin(),(X).rend()
#define eb emplace_back
#define UNIQUE(X) (X).erase(unique(all(X)),(X).end())
#define Endl endl
#define NL <<"\n"

using namespace std;
using ll=long long;
using pii=pair<int,int>;
using pll=pair<ll,ll>;
template<class T> using vv=vector<vector<T>>;
template<class T> inline bool MX(T &l,const T &r){return l<r?l=r,1:0;}
template<class T> inline bool MN(T &l,const T &r){return l>r?l=r,1:0;}
//#undef NUIP
#ifdef NUIP
#include "benri.h"
#else
#define out(args...)
#endif
#ifdef __cpp_init_captures
template<typename T>vector<T> table(int n, T v){ return vector<T>(n, v);}
template <class... Args> auto table(int n, Args... args){auto val = table(args...); return vector<decltype(val)>(n, move(val));}
#endif
const ll MOD=1e9+7; //998244353

bool solveul(const string &s,int ub,int lb){
	const int n=s.size();
	int l=0,r=0;
	rep(i,n){
		if(s[i]=='1'){
			++l; ++r;
		}else if(s[i]=='0'){
			--l; --r;
		}else{
			--l; ++r;
		}
		while(l<lb) l+=2;
		while(r>ub) r-=2;
		if(l>r) return false;
		// out(i,s[i],l,r,1);
	}
	return true;
}

int solven2(const string &s){
	const int n=s.size();
	int re=n;
	rep(i,n+1)if(solveul(s,i,-n)){
		if(solveul(s,i,0)){
			MN(re,i);
			continue;
		}
		int l=0,r=n+1;
		while(r-l>1){
			int m=(l+r)/2;
			(solveul(s,i,-m)?r:l)=m;
		}
		out(i,r,1);
		MN(re,i+r);
	}
	return re;
}

const pii pINF(MOD,-1),nINF(-MOD,-1);
template<class T>
struct MinMax{
	T mn,mx;
	MinMax(T mn,T mx):mn(mn),mx(mx){}
	MinMax(T v):mn(v),mx(v){}
	MinMax():mn(pINF),mx(nINF){}
	MinMax operator+(const MinMax &seg){
		return MinMax(min(mn,seg.mn),max(mx,seg.mx));
	}
  static MinMax e;
};
template<class T> MinMax<T> MinMax<T>::e=MinMax();
template<class T>ostream&  operator<<(ostream &os, const MinMax<T> &seg){ return os<<"("<<seg.mn<<","<<seg.mx<<")";}

template<class T>
struct Add{
	T ad;
  Add(T ad=0):ad(ad){}
	void operator+=(const Add &lfun){
		ad+=lfun.ad;
	}
	bool operator==(const Add &lfun){ return ad==lfun.ad;}

	template<class S>
	void eval(MinMax<S> &s,int l,int r){
		s.mn.X+=ad;
		s.mx.X+=ad;
	}
	static Add e;
};
template<class T> Add<T> Add<T>::e=Add();
template<class T>ostream&  operator<<(ostream &os, const Add<T> &lfun){ return os<<"("<<lfun.ad<<")";}

template<class Seg, class Ope>
struct LazySegTree{
	vector<Seg> segs;
	vector<Ope> opes;
	
	void lazy_eval(int k,int a,int b){
		if(opes[k]==Ope::e) return;
		opes[k].eval(segs[k],a,b);
		if(2*k+2<opes.size()){
			opes[2*k+1]+=opes[k];
			opes[2*k+2]+=opes[k];
		}
		opes[k]=Ope::e;
	}
	void upd_node(int k){segs[k]=segs[2*k+1]+segs[2*k+2];}
	void upd(int l,int r,Ope ope,int k,int a,int b){
		lazy_eval(k,a,b);
		if(b<=l || r<=a) return; 
		if(l<=a && b<=r){
			opes[k]+=ope;
			lazy_eval(k,a,b);
			return;
		}	
		int m=(a+b)/2;
		upd(l,r,ope,2*k+1,a,m);
		upd(l,r,ope,2*k+2,m,b);
		upd_node(k);
	}
	void upd(int l,int r,Ope ope){ upd(l,r,ope,0,0,segs.size()/2);}

	Seg query(int l,int r,int k,int a,int b){
		lazy_eval(k,a,b);
		if(b<=l || r<=a)return Seg::e; 
		if(l<=a && b<=r){return segs[k];}
  
		int m=(a+b)/2;
		Seg vl=query(l,r,2*k+1,a,m);
		Seg vr=query(l,r,2*k+2,m,b);
		upd_node(k);
		return vl+vr;
	}

	LazySegTree(int n){
		int pw=1;
		while(pw<n) pw*=2;
		segs.resize(2*pw);
		opes.resize(2*pw);
	}
	template<class Int>
	LazySegTree(vector<Int> vs){
		int n=vs.size();
		int pw=1;
		while(pw<n) pw*=2;
		segs.resize(2*pw);
		opes.resize(2*pw);
		rep(i,n) segs[pw-1+i]=vs[i];
		rrep(i,pw-1) segs[i]=segs[i*2+1]+segs[i*2+2];
	}
	void operator()(int l,int r,Ope ope){ upd(l,r,ope,0,0,segs.size()/2);};
	Seg operator()(int l,int r){ return query(l,r,0,0,segs.size()/2);};
};

int solve(const string &s){
	const int n=s.size();
	LazySegTree<MinMax<pii>,Add<int>> st(n+1);
	const int N=st.segs.size()/2;
	{
		st.segs[N-1]=MinMax(pii{0,0},pii{0,0});
		int l=0,r=0;
		rep(i,n){
			if(s[i]=='1'){
				++l; ++r;
			}else if(s[i]=='0'){
				--l; --r;
			}else{
				--l; ++r;
			}
			st.segs[N+i]=MinMax(pii{l,i+1},pii{r,i+1});
		}
	}
	auto dec=
		[&](){
			int idx=st(0,N).mn.Y;
			st(idx,N,2);
		};
}


int main(){
  ios_base::sync_with_stdio(false); cin.tie(0);
  cout<<fixed<<setprecision(0);
	string s;
	cin>>s;
	cout<<solven2(s) NL;
	// out(solven2(s),1);
	// out(solve(s),1);
  return 0;
}
