#include <bits/stdc++.h>

using namespace std;
#define int long long
const int MOD = 1000000007;

signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	string S;
	cin >> S;
	int N = S.size();
	vector<int> X(N + 1, 0);
	vector<int> pref_mn(N + 1, 0);
	vector<int> suff_mn(N + 1, 0);
	vector<int> pref_mx(N + 1, 0);
	vector<int> suff_mx(N + 1, 0);
	for (int i = 0; i < N; i++) {
		X[i + 1] = X[i] + (S[i] == '0' ? -1 : 1);
		pref_mn[i + 1] = min(X[i + 1], pref_mn[i]);
		pref_mx[i + 1] = max(X[i + 1], pref_mx[i]);
		//cerr << X[i] << endl;
	}
	suff_mn[N] = X[N];
	suff_mx[N] = X[N];
	for (int i = N - 1; i >= 0; i--) {
		suff_mn[i] = min(suff_mn[i + 1], X[i]);
		suff_mx[i] = max(suff_mx[i + 1], X[i]);
	}
	vector<int> L(N + 1, -1);
	for (int i = N; i >= 0; i--) {
		if (X[i] >= 0) {
			L[X[i]] = i;
		}
	}
	int res = pref_mx.back() - pref_mn.back();
	for (int j = 0; j < 2; j++) {
		int cur = N;
		int mx = pref_mx.back();
		//cerr << "mx = " << mx << endl;
		int INF = (int)1 << 60;
		int smx = -INF;
		int smn = INF;
		while (mx >= 0) {
			int i = mx;
			if (i % 2 != j) {
				i--;
			}
			if (i >= 0 && L[i] != -1) {
				//cerr << cur << " " << L[i] - 1 << endl;
				int ncur = min(cur - 1, L[i] - 1);
				int tmx = -INF;
				int tmn = INF;
				while (cur > ncur || (cur >= 0 && S[cur] != '?')) {
					if (cur >= 0) {
						tmx = max(tmx, X[cur] - 2);
						tmn = min(tmn, X[cur] - 2);
					}
					cur--;
				}
				smx = max(smx - 2, tmx);
				smn = min(smn - 2, tmn);
				if (cur >= 0) {
					int mn = min(pref_mn[cur], smn);
					mx = max(pref_mx[cur], smx);
					//cerr << mx << " " << mn << endl;
					res = min(res, mx - mn);
				} else {
					break;
				}
			}else{
				break;
			}
		}
	}
	cout << res << endl;
}