#include <bits/stdc++.h>
#define FOR(i, a, n) for(ll i = (ll)a; i < (ll)n; i++)
#define FORR(i, n) for(ll i = (ll)n - 1LL; i >= 0LL; i--)
#define rep(i, n) FOR(i, 0, n)
#define ALL(x) begin(x), end(x)
using namespace std;
using ll = long long;
constexpr ll Mod = 998244353;
constexpr ll mod = 1e9 + 7;
constexpr ll inf = 1LL << 60;
const double PI = acos(-1);
template <typename T> constexpr bool chmax(T &a, const T &b) {
    if(a >= b) return false;
    a = b;
    return true;
}
template <typename T> constexpr bool chmin(T &a, const T &b) {
    if(a <= b) return false;
    a = b;
    return true;
}

/*-------------------------------------------*/

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

    string s;
    cin >> s;
    int n = s.size();
    vector<int> h{0};
    for(auto c : s) {
        int cur = h.back() + (c == '1' ? 1 : -1);
        h.push_back(cur);
    }
    vector<int> z = h;
    FORR(i, n) chmax(z[i], z[i + 1]);
    int mx = *max_element(ALL(h));
    int ans = 1 << 30;
    rep(_, 2) {
        int cur = 0, mn = 0;
        rep(i, n) {
            if(s[i] == '?') {
                if(cur + z[i + 1] - h[i] + 2 <= mx)
                    cur++;
                else
                    cur--;
            } else if(s[i] == '0')
                cur--;
            else
                cur++;
            chmin(mn, cur);
        }
        chmin(ans, mx - mn);
        mx++;
    }
    cout << ans << endl;

    return 0;
}