#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];
void solvep(string s) {//stuff with +
	p[0] = 0;
	int gm = 0;
	//cout << s << '\n';
	for(int i = 1; i <= n; i++) {
		p[i] = p[i-1] + (s[i] == '1' ? -1 : 1);
		gm = min(gm, p[i]);
	//	cout << p[i] << " ";
	}
//	cout << '\n';
	sm[n] = p[n];
	for(int i = n-1; i >= 0;i--) sm[i] = min(p[i], sm[i+1]);
	//for(int i = 0; i <= n; i++) cout << sm[i] << " ";
	
	//for(int i = 0; i <= n; i++) cout << p[i] << ' '; cout << '\n';
	int r = 0;
	for(int i = 0; i <= n; i++) {
		sm[i] += r;
		p[i] += r;
		//cout << i << " " << sm[i] << " " << sm[i]-2 << " " << gm << '\n';
		if(s[i] == '?' && sm[i] - 2 >= gm) {
			r-=2;
			//cout <<"at " << i << '\n';
			sm[i] -= 2;
			p[i] -= 2;
			s[i] = '1';
		}
		tt[i] = r;
		if(s[i] == '?') s[i] = '0';
		//cout << p[i] << "\n";
	}
	//for(int i = 0; i <= n; i++) cout << p[i] << ' '; cout << '\n';
	//cout << s << '\n';
	int x = 0, y = 0; r = 0;
	for(auto i : s) {
		if(i == '1') r--;
		else if(i != '.') r++;
		x = min(x, r);
		y = max(y, r);
	}
	ans = min(ans, y-x);
}
int main() {
	cin.tie(0)->sync_with_stdio(0);
	cin >> s;
	n = s.size();
	s = "." + s;
	for(int i = 0; i < 2; i++) {
	memset(p, 0, sizeof p);
	memset(sm, 0, sizeof sm);
	solvep(s);
	memset(p, 0, sizeof p);
	memset(sm, 0, sizeof sm);
	for(auto &i : s) if(i != '?' && i != '.') i ^='0'^'1';
	solvep(s);
	reverse(s.begin(), s.end());
	s.pop_back();
	s = "." + s;
	}
	cout << ans;
}
