#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcountll
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock()/CLOCKS_PER_SEC

const int INF = 1e9;
int solve(string s, int up) {
    int n = s.size();
    vector <int> pref(n + 1);
    for (int i = 0; i < n; ++i) {
        int x;
        if (s[i] == '1')
            x = 1;
        else
            x = -1;

        pref[i + 1] = pref[i] + x;            
    }   

    vector <int> suff(n + 1, -INF);
    for (int i = n - 1; i >= 0; --i)
        suff[i] = max(suff[i + 1], pref[i + 1]);

    int rem = 0, cur = 0;
    int mn = 0;
    for (int i = 0; i < n; ++i) {
        if (s[i] == '?') {
            if (suff[i] + 2 * (rem + 1) <= up) {
                ++rem;
                ++cur;
            }   
            else {
                --cur;
            }   
        }   
        else if (s[i] == '1') {
            ++cur;
        }   
        else {
            --cur;
        }   

        //cout << "cur : " << cur << endl;
        
        mn = min(mn, cur);
    }
    return up - mn;
}   

signed main() {
    #ifdef HOME
    freopen("input.txt", "r", stdin);
    #else
    #define endl '\n'
    ios_base::sync_with_stdio(0); cin.tie(0);
    #endif  

    string s;
    cin >> s;

    int n = s.size();

    int mx = 0, cur = 0;
    for (int i = 0; i < n; ++i) {
        if (s[i] == '1') {
            ++cur;
        }   
        else {
            --cur;
        }   
        mx = max(mx, cur);        
    }   
    
    cout << min(solve(s, mx), solve(s, mx + 1)) << endl;
}