#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int maxn = 1<<20, mod = 1e9 + 7;
string s;
int n, ans = 1<<30;
int p[maxn], sm[maxn], tt[maxn];
//0 => -1 && 1 => 1
int solve() {//initialize to 1
	memset(p, 0, sizeof p);//prefix sum
	memset(sm, 0, sizeof sm);//suffix min of prefix sums
	int g_min = 0, g_max = 0;
	for(int i = 1; i <= n; i++) {
		p[i] = p[i-1] + (s[i] == '0' ? -1 : 1);
		sm[i] = p[i];
	}
	for(int i = n; i--;) sm[i] = min(sm[i], sm[i+1]);
	g_min = sm[0];
	for(int run = 0, i = 0; i <= n; i++) {
		sm[i] += run;
		p[i] += run;
		if(s[i] == '?' && sm[i] - 2 >= g_min) {
			sm[i] -= 2;
			p[i] -= 2;
			run -= 2;
		}
		g_max = max(g_max, p[i]);
	}
	return g_max - g_min;
}
int main() {
	cin.tie(0)->sync_with_stdio(0);
	cin >> s;
	n = s.size();
	s = "." + s;
	int ans = 1<<30;
	for(int i = 0; i < 2; i++) {
		ans = min(ans, solve());
		for(auto &c : s) 
			if(c == '0') c = '1';
			else if(c == '1') c = '0';
		ans = min(ans, solve());
		reverse(1 + s.begin(), s.end());
	}
	cout << ans;
}
