#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define chmax(a, b) a = max(a, b);
#define chmin(a, b) a = min(a, b);
using namespace std;
using ll = long long;
using P = pair<int,int>;

int main() {
	string s;
	cin >> s;
	vector<int> d;
	d.push_back(0);
	int now = 0;
	rep(i, s.size()) {
		now += (s[i] == '1' ? -1 : 1);
		d.push_back(now);
	}
	int mx = 0, mn = 0;
	for(int di: d) {
		chmax(mx, di);
		chmin(mn, di);
	}
	int mxind;
	rep(i, d.size()) {
		if (d[i] == mx) {
			mxind = i;
			break;
		}
	} // smallest index
	int mnind;
	rep(i, d.size()) {
		if (d[i] == mn) {
			mnind = i;
		}
	} // largest index
	if (mxind < mnind) {
		cout << mx - mn << endl;
		return 0;
	}
	int mx2 = mn;
	rep(i, mnind) chmax(mx2, d[i]);
	int ans = 1001001001;
	rep(_, 2) {
		priority_queue<P> q;
		for(int i = mnind + 1; i < d.size(); i++) {
			q.emplace(-d[i], i);
		}
		int cnt = 0;
		int tmx = mn;
		for(int i = mnind; i < mxind; i++) {
			while(q.top().second <= i) q.pop();
			if (s[i] == '?' && -q.top().first - (cnt + 1) * 2 >= mn) {
				cnt += 1;
			}
			chmax(tmx, d[i+1] - cnt * 2);
		}
		chmin(ans, max(tmx, mx2) - mn);
		mn--;
	}
	cout << ans << endl;

	
}
