#include <bits/stdc++.h>
#define pb push_back
#define pll pair <ll, ll>
#define mp make_pair
#define pyshnapyshnakaa ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define x first
#define y second
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#define plll pair <pair <ll, ll>, ll>
#define pllll pair <pair <ll, ll>, pair <ll, ll> >
#define psl pair <string, ll>
#define all(a) a.begin(), a.end()
#define vvl vector <vector <ll> >

typedef long long ll;

typedef long double ld;

using namespace std;

const ll maxn = 1e6;

ll n, m, k, t;

string s;

vector <ll> S;

ll getf(ll mx) {
    // cout << "MX " << mx << endl;
    ll mn = 0;
    ll q;
    ll d = 0;
    ll cur = 0;
    for (q = 0; q < s.length(); q++) {
        // cout << q << " " << S[q] << endl;
        if (s[q] == '0') {
            // cout << "cur " << cur << endl << endl;
            cur--;
            mn = min(mn, cur);
            continue;
        }
        if (s[q] == '1') {
            cur++;
            // cout << "cur " << cur << endl << endl;
            mn = min(mn, cur);
            continue;
        }
        if (S[q] + d + 2 <= mx) {
            // cout << q << " UP" << endl;
            d += 2;
            cur++;
        }
        else {
            cur--;
        }
        // cout << "cur " << cur << endl << endl;
        mn = min(mn, cur);
    }
    // cout << mx - mn << " GETF" << endl << endl;
    return mx - mn;
}

int main() {
    pyshnapyshnakaa;
    ll q, w, e, a, b, c;
    cin >> s;
    n = s.length();
    S.resize(n);
    ll z = 0;
    ll cur = 0;
    for (q = 0; q < s.length(); q++) {
        if (s[q] == '1') {
            cur++;
        }
        else {
            cur--;
        }
        S[q] = cur;
        z = max(z, cur);
    }
    // cout << "Z " << z << endl;
    for (q = n - 2; q >= 0; q--) {
        S[q] = max(S[q], S[q + 1]);
    }
    ll ans = min(getf(z), getf(z + 1));
    cout << ans;
    return 0;
}
