#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
using namespace std;
#define endl '\n'
#define ll long long
#define pi pair<int, int>
#define f first
#define s second

const int mxn = 1000000;
int n;
string s;
int a[mxn], b[mxn];

int sol(int x){
	int r1 = 0, r2 = 0;
	for(int i = 0; i < n; i++){
		r2 += 2 * (s[i] == '?' && b[i] + r2 < x - 1);
		r1 = min(r1, a[i] + r2);
	}
	return x - r1;
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> s;
	n = s.size();
	
	for(int i = 0; i < n; i++){
		a[i] = 2 * (s[i] == '1') - 1 + (i ? a[i - 1] : 0);
	}
	for(int i = n - 1; ~i; i--){
		b[i] = max(a[i], (i < n - 1 ? b[i + 1] : a[i]));
	} 
	int v = max(b[0], 0);
	
	cout << min(sol(v), sol(v + 1)) << endl;

	return 0;
}