#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 5;
typedef long long ll;
template <typename T> void chkmax(T &x, T y) {x = max(x, y); }
template <typename T> void chkmin(T &x, T y) {x = min(x, y); } 
template <typename T> void read(T &x) {
	x = 0; int f = 1;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
	for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
	x *= f;
}
char s[MAXN];
int n, a[MAXN], sMin[MAXN]; bool b[MAXN];
void init() {
	for (int i = 1; i <= n; i++) {
		if (s[i] == '0') a[i] = a[i - 1] - 1;
		else a[i] = a[i - 1] + 1;
		if (s[i] == '?') b[i] = true;
	}
	sMin[n + 1] = n + 5;
	for (int i = n; i >= 0; i--)
		sMin[i] = min(sMin[i + 1], a[i]);
}
int work(int limit) {
	int mns = 0;
	for (int i = 1; i <= n; i++) {
		if (s[i] == '?' && sMin[i] - mns - 2 >= limit) {
			b[i] = false;
			mns += 2;
		} 
		a[i] -= mns;
	}
	int Max = 0;
	for (int i = 1; i <= n; i++)
		chkmax(Max, a[i]);
	return Max - limit;
}
int main() {
	scanf("%s", s + 1);
	n = strlen(s + 1);
	int ans = n + 5, tmp = 0;
	init(); chkmin(ans, work(sMin[0]));
	init(); chkmin(ans, work(sMin[0] - 1));
	cout << ans << endl;
	return 0;
}
