#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define MOD 1000000007
#define MOD2 998244353
#define PRIM 3
#define INF (1 << 29)
#define LINF (1LL << 60)
#define EPS (1e-10)
#define PI 3.1415926535897932384626433832795028
typedef long long Int;
typedef pair<Int, Int> P;
typedef long double Real;
typedef complex<Real> CP;

string s;

bool ok(int x) {
    for (int p = 0; p < 2; p++) {
        bool ng = false;
        int bottom = p, top = x;
        if (top % 2 != p)
            top--;
        for (char c : s) {
            if (c == '0')
                bottom--, top--;
            if (c == '1')
                bottom++, top++;
            if (c == '?')
                bottom--, top++;
            if (bottom < 0)
                bottom += 2;
            if (top > x)
                top -= 2;
            if (top < bottom)
                ng = true;
        }
        if (!ng)
            return true;
    }
    return false;
}

int main() {
    cin >> s;

    int bottom = 0, top = s.size();
    while (top - bottom > 1) {
        int mid = (top + bottom) / 2;
        if (ok(mid))
            top = mid;
        else
            bottom = mid;
    }
    cout << top << endl;
    return 0;
}