#include<bits/stdc++.h>
using namespace std;
#ifndef ONLINE_JUDGE
#define dbg(x...) do{cout << "\033[32;1m" << #x << "->" ; err(x);} while(0)
void err(){cout << "\033[39;0m" << endl;}
template<template<typename...> class T,typename t,typename... A>
void err(T<t> a,A... x){for (auto v:a) cout << v << ' '; err(x...);}
template<typename T,typename... A>
void err(T a,A... x){cout << a << ' '; err(x...);}
#else
#define dbg(...)
#endif
typedef long long ll;
typedef pair<int,int> pi;
typedef vector<int> vi;
template<class T> using vc=vector<T>;
template<class T> using vvc=vc<vc<T>>;
template<class T> void mkuni(vector<T>&v)
{
    sort(v.begin(),v.end());
    v.erase(unique(v.begin(),v.end()),v.end());
}
template<class T>
void print(T x,int suc=1)
{
    cout<<x;
    if(suc==1) cout<<'\n';
    else cout<<' ';
}
template<class T>
void print(const vector<T>&v,int suc=1)
{
    for(int i=0;i<v.size();i++)
        print(v[i],i==(int)(v.size())-1?suc:2);
}
const int INF=0x3f3f3f3f;

string s;
int solve(int x)
{
	int n=s.length();
	vi pre(n);
	vi suf(n);
	for(int i=0;i<n;i++)
	{
		if(s[i]=='1') pre[i]=1;
		else pre[i]=-1;
		if(i) pre[i]+=pre[i-1];
	}
	for(int i=n-1;i>=0;i--)
	{
		suf[i]=pre[i];
		if(i!=n-1) suf[i]=max(suf[i],suf[i+1]);
	}
	int mi=0;
	int cur=0;
	for(int i=0;i<n;i++)
	{
		if(s[i]=='?'&&suf[i]+2+cur<=x)
			cur+=2;
		mi=min(mi,pre[i]+cur);
	}
	return x-mi;
}
int main()
{
	cin>>s;
	if(s.length()==1)
	{
		print(1);
		return 0;
	}
	int cur=0,mx=0;
	for(auto u:s)
	{
		if(u=='?'||u=='0')
			cur--;
		else cur++;
		mx=max(mx,cur);
	}
	//dbg(mx);
	//for(int i=mx;i<mx+20;i++) dbg(i,solve(i));
	print(min(solve(mx),solve(mx+1)));
}
