#include "bits/stdc++.h"
using namespace std;
typedef long long li;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    string s;
    cin >> s;

    li min_maximum = 0, cur = 0;
    vector<li> min_offset(1, 0);
    for (int i = 0; i < s.size(); ++i) {
        if (s[i] == '1') {
            cur += 1;
        } else {
            cur -= 1;
        }
        min_offset.emplace_back(cur);
        min_maximum = max(cur, min_maximum);
    }
    vector<li> back_max(s.size() + 1, min_offset.back());
    for (int i = s.size() - 1; i >= 0; --i) {
        back_max[i] = max(back_max[i + 1], min_offset[i]);
    }

    li ans = s.size();
    for (int ub = min_maximum; ub < min_maximum + 2; ++ub) {
        li cur = 0, maximum = 0, minimum = 0;
        for (int i = 0; i < s.size(); ++i) {
            if (s[i] == '0') {
                cur -= 1;
            } else if (s[i] == '1') {
                cur += 1;
            } else {
                if (cur + 1 + max(0LL, back_max[i + 1] - min_offset[i + 1]) > ub) {
                    cur -= 1;
                } else {
                    cur += 1;
                }
            }
            maximum = max(maximum, cur);
            minimum = min(minimum, cur);
        }
        ans = min(ans, maximum - minimum);
    }

    cout << ans << endl;

    return 0;
}