#include <bits/stdc++.h>
using namespace std;

#define N 1000100

char s[N];

int n, z, p[N], zp[N], zm[N];

int calc(int m) {
    int mn = 0;
    for (int i = 1; i <= n; i ++) {
        p[i] = p[i-1];
        if(s[i] == '0') p[i] --;
        else if(s[i] == '1') p[i] ++;
        else if(p[i] + zm[i] - zp[i] < m) p[i] ++;
        else p[i] --;
        mn = min(mn, p[i]);
    }
    return m - mn;
}

int main() {
    scanf("%s", s + 1);
    n = strlen(s + 1);
    for(int i = 1; i <= n; i ++) {
        zp[i] = zp[i-1];
        if(s[i] == '1') zp[i] ++;
        else zp[i] --;
        z = max(z, zp[i]);
    }
    zm[n+1] = -1e9;
    for(int i = n; i >= 1; i --) zm[i] = max(zm[i+1], zp[i]);
    printf("%d\n", min(calc(z), calc(z + 1)));
}
