#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define FOR(i,n,m) for(int i=(int)(n); i<=(int)(m); i++)
#define RFOR(i,n,m) for(int i=(int)(n); i>=(int)(m); i--)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define RITR(x,c) for(__typeof(c.rbegin()) x=c.rbegin();x!=c.rend();x++)
#define setp(n) fixed << setprecision(n)

template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }

#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll,ll>
#define pi pair<int,int>

#define all(a) (a.begin()),(a.end())
#define rall(a) (a.rbegin()),(a.rend())
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define ins insert

using namespace std;

const int INF=1e9;

//-------------------------------------------------

int main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    string S; cin>>S;
    int N = S.size();
    vi sum(N+1);
    rep(i,N){
        if (S[i]=='0') sum[i+1]=sum[i]+1;
        else sum[i+1]=sum[i]-1;
    }
    vi maxi(N+1,-INF);
    RFOR(i,N-1,0){
        maxi[i] = max(maxi[i+1],sum[i+1]);
    }

    auto getDiff = [&](int x){
        int res=0;
        int c=0;
        rep(i,N){
            if (S[i]=='?' && maxi[i]+c+2<=x) c+=2;
            chmin(res,sum[i+1]+c);
        }
        return x-res;
    };
    int minMax = max(0,maxi[0]);
    int ans=min(getDiff(minMax), getDiff(minMax+1));
    cout<<ans<<"\n";
    return 0;
}
