#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;

int main(void){
    string S;
    cin >> S;
    int Zbuf=0;
    int Z=0;
    multiset<int, greater<int>> csum;
    for(int i=0; i<S.size(); i++){
        if(S[i] == '1'){
            Zbuf++;
        }else Zbuf--;
        Z=max(Z, Zbuf);
        csum.insert(Zbuf);
    }
    Zbuf = 0;
    int Zbuf0 = 0;
    int Zbuf1 = 0;
    int fZ0 = 0;
    int fZ1 = 0;
    int Zbuf0diff = 0;
    int Zbuf1diff = 0;
    for(int i=0; i<S.size(); i++){
        switch(S[i]){
            case '0':
                Zbuf--;
                Zbuf0--;
                Zbuf1--;
                break;
            case '1':
                Zbuf++;
                Zbuf0++;
                Zbuf1++;
                break;
            default:
                Zbuf--;
                int MaxRight = *(csum.begin());
                if(MaxRight+Zbuf0diff+2 <= Z){
                    Zbuf0++;
                    Zbuf0diff += 2;
                }else{
                    Zbuf0--;
                }
                if(MaxRight+Zbuf1diff+2 <= Z+1){
                    Zbuf1++;
                    Zbuf1diff += 2;
                }else{
                    Zbuf1--;
                }
        }
        fZ0=min(fZ0, Zbuf0);
        fZ1=min(fZ1, Zbuf1);
        csum.erase(csum.find(Zbuf));
    }
    cout << min(Z-fZ0, Z+1-fZ1) << endl;
    return 0;
}