#include <bits/stdc++.h>
using namespace std;
using lint     = long long;
const lint inf = 1LL << 60;
const lint mod = 1000000007;

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    string s;
    cin >> s;
    int n      = s.size();
    lint ok    = n;
    lint ng    = 0;
    auto check = [&](lint mid) {
        for (int i = 0; i < 2; ++i) {
            int a = i;
            int b = mid;
            if (a % 2 != b % 2)
                b--;
            bool ok = true;
            for (int j = 0; j < n; ++j) {
                if (s[j] == '1') {
                    a++, b++;
                } else if (s[j] == '0') {
                    a--, b--;
                } else {
                    a--, b++;
                }
                if (a < 0)
                    a += 2;
                if (b > mid)
                    b -= 2;
                if (a > b) {
                    ok = false;
                    break;
                }
            }
            if (ok)
                return true;
        }
        return false;
    };
    while (abs(ok - ng) != 1) {
        lint mid               = (ok + ng) / 2;
        (check(mid) ? ok : ng) = mid;
    }
    cout << ok << "\n";
    return 0;
}