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

const int maxn = 1000010;
int n, suf[maxn];
char s[maxn];

int main() {
    scanf("%s", s + 1), n = strlen(s + 1);
    int mx = 0;
    for (int i = 1, cur = 0; i <= n; i++) {
        if (s[i] ^ '1') cur++;
        else mx = min(mx, --cur);
    }
    auto chk = [&](int x) {
        for (int i = n; i; i--) {
            suf[i] = min(0, suf[i + 1] + (s[i] ^ '1' ? 1 : -1));
        }
        int y = 0;
        for (int i = 1, cur = 0; i <= n; i++) {
            if (s[i] == '0') cur++;
            else if (s[i] == '1') cur--;
            else if (cur - 1 + suf[i + 1] >= x) cur--;
            else cur++;
            y = max(y, cur);
        }
        return y - x;
    };
    printf("%d\n", min(chk(mx), chk(mx - 1)));
    return 0;
}