#include <bits/stdc++.h>

#define I inline
#define fi first
#define se second
#define R register
#define LL long long
#define mp make_pair
#define reg register int
#define pii pair<int,int>
#define fo(i, a, b) for(reg i = a; i <= b; i++)
#define fd(i, a, b) for(reg i = a; i >= b; i--)
#define cr const reg&
using namespace std;
const int inf = 2147483647;
const int N = 1e6 + 1;

I int _max(cr x, cr y) {return x > y ? x : y;}
I int _min(cr x, cr y) {return x < y ? x : y;}
I LL read() {
	LL x = 0, f = 1; char ch = getchar();
	while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
	while(ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
	return x * f;
}
I void ptt(LL x) {if(x >= 10) ptt(x / 10); putchar(x % 10 + '0');}
I void put(LL x) {x < 0 ? putchar('-'), ptt(-x) : ptt(x);}
I void pr1(LL x) {put(x), putchar(' ');}
I void pr2(LL x) {put(x), puts("");}

char ss[N], tt[N];
int n, s[N], suf[N];

I int calc(cr mx) {
	reg hh = 0;
	fo(i, 1, n) {
		tt[i] = ss[i];
		if(ss[i] != '?') continue;
		if(suf[i] + 2 * (hh + 1) <= mx) tt[i] = '1', hh++;
		else tt[i] = '0';
	} reg h1 = 0, h2 = 0; hh = 0;
	fo(i, 1, n) hh += tt[i] == '1' ? 1 : -1, h1 = _max(h1, hh), h2 = _min(h2, hh);
	return h1 - h2;
}

int main() {
	scanf("%s", ss + 1);
	n = strlen(ss + 1);
	reg mx = 0;
	fo(i, 1, n) s[i] = s[i - 1] + (ss[i] == '1' ? 1 : -1), mx = _max(mx, s[i]);
	suf[n] = s[n]; fd(i, n - 1, 1) suf[i] = _max(suf[i + 1], s[i]);
	pr2(calc(mx));
	return 0;
}

