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

using namespace std;

#define ll long long 
const int N = 1e6+7;

char s[N];
int len;
int sum[N],maxn[N],n;

int get(int p){
	int ans = 0,tot = 0;
	for(int i = 1; i <= n; i++){
		if(s[i] == '?' && tot + 2 + maxn[i] <= p) tot += 2;
		ans = min(sum[i] + tot,ans);
	}
	return ans;
}

int main(){
	cin >> (s+1);
	n = strlen(s+1);
	for(int i = 1; i <= n; i++)
		if(s[i] == '1') sum[i] = sum[i-1] + 1;
		else sum[i] = sum[i-1] - 1;
	maxn[n] = sum[n];
	for(int i = n-1; i > 0; i--) maxn[i] = max(maxn[i+1],sum[i]);
	int m = max(0,maxn[1]);
	cout << min(m-get(m),m+1-get(m+1));
} 