#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

bool p[1000009];
int a[1000009], n, mni, mxi, ul;

bool ps(int dl)
{
	int l = 0, r = 0;
	for (int i = 0; i <= n; i++) {
		int u = (ul - a[i]) / 2, d = (dl - a[i] + 1) / 2;
		if (p[i]) r++;
		l = max(l, d); r = min(r, u);
		if (l > r) return false;
	}
	return true;
}

int bs(int l, int r)
{
	int md = (l + r) / 2;
	if (r == l + 1 && md == l) md = r;
	if (l >= r) return md;
	if (ps(md)) return bs(md, r);
	else return bs(l, md - 1);
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	string s; cin >> s;
	n = s.length();
	for (int i = 0; i < n; i++) {
		if (s[i] == '?') {
			s[i] = '0';
			p[i + 1] = true;
		}
		if (s[i] == '0')
			a[i + 1] = a[i] - 1;
		else
			a[i + 1] = a[i] + 1;
	}
	mxi = 0, mni = 0;
	for (int i = 1; i <= n; i++) {
		if (a[mxi] <= a[i])
			mxi = i;
		if (a[mni] > a[i])
			mni = i;
	}
	int ans = a[mxi] - a[mni];
	if (mni <= mxi) {
		cout << ans << '\n';
		return 0;
	}
	ul = a[mxi];
	ans = min(ans, ul - bs(a[mni], a[mxi]));
	ul = a[mxi] + 1;
	ans = min(ans, ul - bs(a[mni], a[mxi]));
	cout << ans << '\n';
	return 0;
}