/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author cherrypi59
 */

#pragma GCC optimize ("O2")
#pragma GCC optimize ("tree-vectorize")
#pragma GCC target ("sse4")

#include <bits/stdc++.h>
#define FOR(i, a, b) for(int i=(a);i<(b);++i)
#define FFOR(i, a, b) FOR(i, a, b+1)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) FOR(i, 1, n+1)
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define LEN(x) (int)(x).size()
#define DUMP(x) cerr<<__LINE__<<' '<<#x<<"="<<(x)<<endl;
#define pb push_back
#define eb emplace_back
#define mp make_pair

using namespace std;
using lint = long long;
using pii =  pair<int, int>;
using pll =  pair<lint, lint>;
template <typename T> using vc = vector<T>;
template <typename T> using vvc = vector<vector<T>>;
template <typename T> inline bool chmax(T &a, T b){if(a<b){a = b; return true;} return false;}
template <typename T> inline bool chmin(T &a, T b){if(a>b){a = b; return true;} return false;}

constexpr lint ten(int n) {return n==0 ? 1 : ten(n-1)*10;}

class B01Unbalanced {
public:
    int N;
    string S;
    vc<int> sums, suff;

    int f(int mx){
        int d = 0, mn = 0;
        REP(i, N){
            if(S[i]=='?' and suff[i+1]+d+2<=mx) d += 2;
            chmin(mn, sums[i+1]+d);
        }
        return mx - mn;
    }

    void solve(std::istream& in, std::ostream& out){
        ios_base::sync_with_stdio(false);
        in.tie(nullptr), out.tie(nullptr);
        in>>S;
        N = LEN(S);
        sums.resize(N+1, 0);
        suff.resize(N+1, 0);
        REP(i, N){
            sums[i+1] = sums[i];
            if(S[i]=='0' or S[i]=='?') sums[i+1]--;
            else sums[i+1]++;
        }
        suff[N] = sums[N];
        for(int i=N-1;0<=i;--i) suff[i] = max(suff[i+1], sums[i]);
        int z = *max_element(ALL(sums));
        out<<min(f(z), f(z+1))<<'\n';
    }
};


int main() {
	B01Unbalanced solver;
	std::istream& in(std::cin);
	std::ostream& out(std::cout);
	solver.solve(in, out);
	return 0;
}
