#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
typedef long long Int;


int main() {
    string S;
    cin >> S;
    Int N;
    N = S.size();

    vector<Int> v(N, 0), sum(N, 0), m(N, 0);
    Int Z = 0;

    rep(i,N) {
        if (S[i] == '1') v[i] = 1;
        else v[i] = -1;
    }
    sum[0] = v[0];
    Z = max(Z, v[0]);
    rep1(i,N-1) {
        sum[i] = sum[i-1] + v[i];
        Z = max(Z, sum[i]);
    }

    //cout << Z << endl;

    m[N-1] = sum[N-1];
    Int tmp = sum[N-1];
    rep(i,N-1) {
        if (tmp >= sum[N-2-i]) m[N-2-i] = tmp;
        else {
            tmp = sum[N-2-i];
            m[N-2-i] = tmp;
        }
    }

    Int tmp1 = 0, min1 = 0, max1 = 0, tmp2 = 0, min2 = 0, max2 = 0;

    rep(i,N) {
        if (S[i] == '?') {
            if (tmp1 + 1 + m[i] - sum[i] <= Z) {
                tmp1++;
            }
            else tmp1--;
        }
        else {
            tmp1 += v[i];
        }
        min1 = min(min1, tmp1);
        max1 = max(max1, tmp1);
    }

    rep(i,N) {
        if (S[i] == '?') {
            if (tmp2 + 1 + m[i] - sum[i] <= Z+1) {
                tmp2++;
            }
            else tmp2--;
        }
        else {
            tmp2 += v[i];
        }
        min2 = min(min2, tmp2);
        max2 = max(max2, tmp2);
    }

    Int ans = 0;
    ans = min(max1-min1, max2-min2);

    cout << ans << endl;
}
