#include <bits/stdc++.h>

using namespace std;

typedef double D;
typedef long long int LL;

#define st first
#define nd second
#define PLL pair <LL, LL>
#define PII pair <int, int>

const int N = 1e6 + 7;
const int MX = 1e9 + 7;
const LL INF = 1LL * MX * MX;

int n;
char in[N];
int limit[N];

int check(int v){
	int zeroes = 0, ones = 0, mx = 0;
	for(int i = 1; i <= n; ++i){
		char c = in[i];
		if(c == '0'){
			++zeroes;
		}
		else if(c == '1'){
			++ones;
			mx = max(mx, ones - zeroes);
		}
		else{
			if(zeroes - ones + limit[i] + 1 <= v)
				++zeroes;
			else{
				++ones;
				mx = max(mx, ones - zeroes);
			}
		}
	}
	
	return mx + v;
}

int main(){
	scanf("%s", in + 1);
	n = strlen(in + 1);
	
	int mx = 0;
	for(int i = n; i >= 1; --i){
		limit[i] = mx;
		if(in[i] == '0'){
			++mx;
		}
		else{
			--mx;
			if(mx < 0)
				mx = 0;
		}
	}
	
	int ans = N;	
	int s = mx, e = n;

	while(e - s >= 5){
		int mid = (s + e) / 2;
		int Left = min(check(mid - 1), check(mid));
		int Right = min(check(mid + 1), check(mid + 2));
		ans = min(ans, min(Left, Right));

		if(Left < Right)
			e = mid;
		else
			s = mid + 1;
	}
	
	for(int i = s; i <= e; ++i)
		ans = min(ans, check(i));
	printf("%d\n", ans);
	return 0;
}