#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <vector>

#define X first
#define Y second
#define PB push_back

using namespace std;

typedef long long ll;
typedef pair < int, int > pii;
typedef vector < int > vi;

const int N = 1e6 + 500;
const int INF = 0x3f3f3f3f;
const int MOD = 998244353;

int mini = 0, maxi = 0, n;
char s[N];
int desno[N];

int probaj(int x){
	if(desno[0] < x)
		return n;
	int cur = 0, gran = 0;
	for(int i = 0;i < n;i++){
		if(s[i] == '1'){
			cur++;
		}
		else if(s[i] == '0'){
			cur--;
		}
		else{
			if(cur + desno[i + 1] > x)
				cur--;
			else
				cur++;
		}
		gran = max(cur, gran);
	}
	return gran - x;
}

int main(){
	scanf("%s", s);
	n = strlen(s);
	desno[n] = 0;
	for(int i = n - 1;i >= 0;i--){
		if(s[i] == '1' || s[i] == '?'){
			desno[i] = min(desno[i + 1] + 1, 0);
		}
		else{
			desno[i] = desno[i + 1] - 1;
		}
	}
	int sol = n;
	for(int x = desno[0];x >= desno[0] - 50;x--)
		sol = min(sol, probaj(x));
	printf("%d\n", sol);
	return 0;
}




