//~ while (clock()<=69*CLOCKS_PER_SEC)
//~ #pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
//~ #pragma GCC target ("avx2")
//~ #pragma GCC optimize("Ofast")
//~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//~ #pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

#define shandom_ruffle random_shuffle

using ll=long long;
using pii=pair<int,int>;
using pll=pair<ll,ll>;
using vi=vector<int>;
using vll=vector<ll>;
const int nax=1007*1007;

int n;
char wcz[nax];

set<pii> setel;
int dod;

pii pocz()
{
	auto it=setel.begin();
	pii x=*it;
	return {x.first+dod, x.second+dod};
}

pii kon()
{
	auto it=setel.end();
	it--;
	pii x=*it;
	return {x.first+dod, x.second+dod};
}

void wstaw(pii v)
{
	v.first-=dod;
	v.second-=dod;
	setel.insert(v);
}

void usun(pii v)
{
	v.first-=dod;
	v.second-=dod;
	setel.erase(v);
}

int check(int v)
{
	setel.clear();
	setel.insert({0, 0});
	dod=0;
	for (int i=1; i<=n; i++)
	{
		if (wcz[i]=='0')
		{
			dod++;
			if (kon().first>=0)
			{
				int naj=0;
				while(!setel.empty() && kon().first>=0)
				{
					naj=max(naj, kon().second);
					usun(kon());
				}
				wstaw({0, naj});
			}
		}
		if (wcz[i]=='1')
		{
			dod--;
			if (pocz().second<=0)
			{
				int naj=0;
				while(!setel.empty() && pocz().second<=0)
				{
					naj=min(naj, pocz().first);
					usun(pocz());
				}
				wstaw({naj, 0});
			}
		}
		if (wcz[i]=='?')
		{
			dod++;
			wstaw({pocz().first-2, pocz().second-2});
			//~ prz.first--;
			//~ prz.second++;
		}
		while(!setel.empty() && pocz().first<-v)
			usun(pocz());
		while(!setel.empty() && kon().second>v)
			usun(kon());
		if (setel.empty())
			return 0;
	}
	return 1;
}

int main()
{
	scanf("%s", wcz+1);
	n=strlen(wcz+1);
	int bsa=1;
	int bsb=n;
	while(bsa<bsb)
	{
		int bss=(bsa+bsb)>>1;
		if (check(bss))
			bsb=bss;
		else
			bsa=bss+1;
	}
	printf("%d\n", bsa);
	return 0;
}
