# include <bits/stdc++.h>
using namespace std;
namespace Base{
	# define mr make_pair
	typedef long long ll;
	typedef double db;
	const int inf = 0x3f3f3f3f, INF = 0x7fffffff;
	const ll  infll = 0x3f3f3f3f3f3f3f3fll, INFll = 0x7fffffffffffffffll;
	template<typename T> void read(T &x){
    	x = 0; int fh = 1; double num = 1.0; char ch = getchar();
		while (!isdigit(ch)){ if (ch == '-') fh = -1; ch = getchar(); }
		while (isdigit(ch)){ x = x * 10 + ch - '0'; ch = getchar(); }
	    if (ch == '.'){
	    	ch = getchar();
	    	while (isdigit(ch)){num /= 10; x = x + num * (ch - '0'); ch = getchar();}
		}
		x = x * fh;
	}
	template<typename T> void chmax(T &x, T y){x = x < y ? y : x;}
	template<typename T> void chmin(T &x, T y){x = x > y ? y : x;}
}
using namespace Base;

const int N = 1000100; 
char s[N];
int n;
int bak[N];
int main(){
	scanf("\n%s", s + 1);
	n = strlen(s + 1);
	int mx = 0, pre = 0, mn, lim, ans;
	for (int i = 1; i <= n; i++){
		if (s[i] == '1') pre++;
			else pre--;
		mx = max(pre, mx);
	}
	for (int i = n; i >= 1; i--){
		if (s[i] == '1') bak[i] = 1 + bak[i + 1];
			else bak[i] = -1 + bak[i + 1];
		bak[i] = max(bak[i], 0);
	}
	
	lim = mx, pre = 0, mn = 0;
	for (int i = 1; i <= n; i++){
		if (s[i] == '1') pre++;
		if (s[i] == '0') pre--;
		if (s[i] == '?'){
			if (pre + 1 + bak[i + 1] <= lim) pre++;
				else pre--;
		}
		mn = min(mn, pre);
	}
	ans = lim - mn;
	
	lim = mx + 1, pre = 0, mn = 0;
	for (int i = 1; i <= n; i++){
		if (s[i] == '1') pre++;
		if (s[i] == '0') pre--;
		if (s[i] == '?'){
			if (pre + 1 + bak[i + 1] <= lim) pre++;
				else pre--;
		}
		mn = min(mn, pre);
	}
	ans = min(ans, lim - mn);
	cout << ans << endl;
    return 0;
}
