#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define all(aaa) aaa.begin(), aaa.end()

const int N = 1e6 + 5;
string s;
int n;
int mx_suf[N];

int f(int z) {
    int mn = 0, suf = 0;

    for (int i = 0; i < n; i++) {
        for (char c = '1'; c >= '0'; c--) {
            int d = (c == '1' ? 1 : -1);
            if ((c == s[i] || s[i] == '?') &&
                suf + d + mx_suf[i + 1] <= z) {
                suf += d;
                break;
            }
        }
        mn = min(mn, suf);
    }

    return z - mn;
}

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

    cin >> s;
    n = s.size();

    for (int i = n - 1; i >= 0; i--) {
        mx_suf[i] = max(mx_suf[i + 1] + (s[i] == '1' ? 1 : -1), 0);
    }

    cout << min(f(mx_suf[0]), f(mx_suf[0] + 1));

	return 0;
}