#pragma GCC optimize ("O3")
// #pragma GCC target ("avx")
#include<stdio.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<string.h>

#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define NDEBUG
#define eprintf(...) do {} while (0)
#endif
#include<cassert>

using namespace std;

typedef long long LL;
typedef vector<int> VI;

#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(),i##_end=(c).end();i!=i##_end;++i)

template<class T> inline void amin(T &x, const T &y) { if (y<x) x=y; }
template<class T> inline void amax(T &x, const T &y) { if (x<y) x=y; }
template<class Iter> void rprintf(const char *fmt, Iter begin, Iter end) {
    for (bool sp=0; begin!=end; ++begin) { if (sp) putchar(' '); else sp = true; printf(fmt, *begin); }
    putchar('\n');
}

int N;
char S[1000011];
int H[1000011];

int ans;
void solve(int height) {
    int cur = 0;
    int mi = 0;
    int ma = 0;
    REP (i, N) {
	if (S[i] == '0') cur--;
	else if (S[i] == '1') cur++;
	else {
	    if (cur + 1 <= height && cur + 1 + H[i+1] <= height) {
		cur++;
	    } else {
		cur--;
	    }
	}

	amin(mi, cur);
	amax(ma, cur);
    }

    amin(ans, ma - mi);
}

void MAIN() {
    scanf("%s", S);
    N = strlen(S);

    ans = 1<<29;

    solve(0);
    REP (a, 2) {
	REP (b, 2) {
	    int cur = 0;
	    int ma = 0;
	    for (int i=N; i--;) {
		if (S[i] == '1') cur--;
		else cur++;
		amax(ma, cur);
		H[i] = ma - cur;
	    }

	    int height = max(0, *max_element(H, H+N));
	    for (int d=-10; d<=10; d++) {
		if (0 <= height + d) solve(height + d);
	    }

	    reverse(S, S+N);
	}
	REP (i, N) if (S[i] != '?') S[i] ^= 1;
    }

    printf("%d\n", ans);
}

int main() {
    int TC = 1;
//    scanf("%d", &TC);
    REP (tc, TC) MAIN();
    return 0;
}

