/*
    author:  Maksim1744
    created: 07.06.2020 15:13:18
*/

#include <bits/stdc++.h>

using namespace std;

#define ll   long long
#define ld   long double

#define mp   make_pair
#define pb   push_back
#define eb   emplace_back

#define sum(a)     ( accumulate ((a).begin(), (a).end(), 0ll))
#define mine(a)    (*min_element((a).begin(), (a).end()))
#define maxe(a)    (*max_element((a).begin(), (a).end()))
#define mini(a)    ( min_element((a).begin(), (a).end()) - (a).begin())
#define maxi(a)    ( max_element((a).begin(), (a).end()) - (a).begin())
#define lowb(a, x) ( lower_bound((a).begin(), (a).end(), (x)) - (a).begin())
#define uppb(a, x) ( upper_bound((a).begin(), (a).end(), (x)) - (a).begin())

template<typename T>             vector<T>& operator--            (vector<T>& v){for (auto& i : v) --i;            return  v;}
template<typename T>             vector<T>& operator++            (vector<T>& v){for (auto& i : v) ++i;            return  v;}
template<typename T>             istream& operator>>(istream& is,  vector<T>& v){for (auto& i : v) is >> i;        return is;}
template<typename T>             ostream& operator<<(ostream& os,  vector<T>& v){for (auto& i : v) os << i << ' '; return os;}
template<typename T, typename U> istream& operator>>(istream& is, pair<T, U>& p){is >> p.first >> p.second;        return is;}
template<typename T, typename U> ostream& operator<<(ostream& os, pair<T, U>& p){os << p.first << ' ' << p.second; return os;}
template<typename T, typename U> pair<T,U> operator-(pair<T,U> a, pair<T,U> b){return mp(a.first-b.first, a.second-b.second);}
template<typename T, typename U> pair<T,U> operator+(pair<T,U> a, pair<T,U> b){return mp(a.first+b.first, a.second+b.second);}
template<typename T, typename U> void umin(T& a, U b){if (a > b) a = b;}
template<typename T, typename U> void umax(T& a, U b){if (a < b) a = b;}

#ifdef HOME
#define TAG_LENGTH       25
#define LR_LEFT          left
#define LR_RIGHT         right
#define LR_VALUE         value
#define LR_SECOND_VALUE  mn
#include "C:/C++ libs/print.cpp"
#else
#define showl            42;
#define shows            42;
#define show(...)        42;
#define showm(...)       42;
#define showt(...)       42;
#define printTree(...)   42;
#define printGraph(...)  42;
#define printLRTree(...) 42;
#define printMatrix(...) 42;
#define printWGraph(...) 42;
#define debug     if (false)
#endif

int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

    string s;
    cin >> s;
    if (s == string(s.size(), '?')) {
        cout << 1 << '\n';
        return 0;
    }
    int ind = 0;
    while (s[ind] == '?') ++ind;
    s = s.substr(ind, s.size());
    vector<int> pos;
    for (int i = 0; i < s.size(); ++i) {
        if (s[i] == '?')
            pos.pb(i);
    }
    vector<string> subs;
    pos.pb(s.size());
    subs.pb(s.substr(0, pos[0]));
    for (int i = 0; i + 1 < pos.size(); ++i) {
        subs.pb(s.substr(pos[i] + 1, pos[i + 1] - pos[i] - 1));
    }
    vector<int> dif(subs.size());
    vector<int> mn(subs.size(), 0);
    vector<int> mx(subs.size(), 0);
    for (int i = 0; i < subs.size(); ++i) {
        string s = subs[i];
        int balance = 0;
        for (auto c : s) {
            if (c == '0') ++balance;
            else --balance;
            mn[i] = min(mn[i], balance);
            mx[i] = max(mx[i], balance);
        }
        dif[i] = balance;
    }
    int l = 0, r = s.size() + 10;
    while (r - l > 1) {
        int c = (l + r) / 2;
        int a = 0, b = c;
        int even = -1;
        a = -mn[0];
        b = c - mx[0];
        a += dif[0];
        b += dif[0];
        a = max(a, 0);
        b = min(b, c);
        for (int i = 1; i < subs.size(); ++i) {
            if (a > b) break;
            if (a == b && even == -1) even = a % 2;
            // if (c == 2) shows;
            // if (c == 2) show(a, b);
            --a;
            ++b;
            if (even != -1) even = 1 - even;
            a = max(a, 0);
            b = min(b, c);
            a = max(a, -mn[i]);
            b = min(b, c - mx[i]);
            if (even != -1) {
                while (a % 2 != even) ++a;
                while (b >= 0 && b % 2 != even) --b;
            }
            if (a > b) break;
            if (a == b) {
                a += dif[i];
                b += dif[i];
                even = abs(b) % 2;
                a = max(a, 0);
                b = min(b, c);
            } else {
                if (even == -1) {
                    a += dif[i];
                    b += dif[i];
                    a = max(a, 0);
                    b = min(b, c);
                } else {
                    a += dif[i];
                    b += dif[i] + 1;
                    a = max(a, 0);
                    b = min(b, c);
                    even = (even + abs(dif[i])) % 2;
                }
            }
            // a = max(a, dif[i] - mn[i]);
            // b = min(b, c - (mx[i] - dif[i]));
        }
        if (even != -1) {
            while (a % 2 != even) ++a;
            while (b >= 0 && b % 2 != even) --b;
        }
        if (a > b)
            l = c;
        else
            r = c;
    }
    cout << r << '\n';

    return 0;
}


Measured memory_bytes: 51104