#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i, x, n) for (int i = x; i < (int)(n); ++i)

int const N = 1000000;
int n;
char s[N + 1];

bool ok(int m){
	int l[2] = { 0, 1 }, r[2] = { m, m };
	--r[m & 1 ^ 1];
	f(i, 0, n)f(j, 0, 2){
		if (s[i] == '?')--l[j], ++r[j];
		else if (s[i] == '0')--l[j], --r[j];
		else ++l[j], ++r[j];
		if (l[j] < 0)l[j] += 2;
		if (r[j] > m)r[j] -= 2;
		if (l[j] > m || r[j] < 0)r[j] = 1e9;
	}
	return r[0] <= m || r[1] <= m;
}

int main(){
	scanf("%s", s);
	n = strlen(s);
	int l = 1, r = n;
	while (r > l){
		int m = l + r >> 1;
		if (ok(m))r = m;
		else l = m + 1;
	}
	printf("%d\n", l);
}