#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<(b);++i)
#define debug(x) cerr<<#x<<": "<<x<<endl
#define trav(a,x) for(auto &a:x)
#define all(x) x.begin(),x.end()
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    string s;
    cin>>s;
    int n = sz(s);
    int lo = 1, hi = n;
    while(lo<hi) {
        int mid = (lo+hi)/2;
        bool either = false;
        rep(par,0,2) {
            int l = par;
            int r = mid-(mid%2);
            if(mid%2==0) r-=par;
            else r+=par;
            bool ok = true;
            rep(i,0,n) {
                if(s[i]=='0') {
                    ++l;
                    ++r;
                    if(r>mid) r-=2;
                }
                else if(s[i]=='1') {
                    --r;
                    --l;
                    if(l<0) l+=2;
                }
                else {
                    if(r+1<=mid) ++r;
                    else --r;
                    if(l-1>=0) --l;
                    else ++l;
                }
                if(l>r) {
                    ok = false;
                    break;
                }
            }
            if(ok) either = true;
        }
        if(either) hi = mid;
        else lo = mid+1;
    }
    cout<<lo<<"\n";
}
