#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

char s[1000001];
int a[1000000];

int calc(int n, int m) {
    int x = 0, y = 0, i;
    
    for (i = 0; i < n; i++) {
        if (s[i] == '1' || (s[i] == '?' && x + 1 + a[i] <= m)) {
            x++;
        } else {
            x--;
        }
        
        y = min(y, x);
    }
    
    return m - y;
}

int main() {
    int n, m = 0, x = 0, y = 0, i;
    
    scanf("%s", s);
    
    n = strlen(s);
    
    for (i = 0; i < n; i++) {
        if (s[i] == '1') {
            x++;
        } else {
            x--;
        }
        
        m = max(m, x);
    }
    
    x = 0;
    
    for (i = n - 1; i >= 0; i--) {
        a[i] = y - x;
        
        if (s[i] == '1') {
            x--;
        } else {
            x++;
        }
        
        y = max(y, x);
    }
    
    printf("%d\n", min(calc(n, m), calc(n, m + 1)));
    
    return 0;
}
