#include <bits/stdc++.h>
 
#define endl '\n'
#define fi first
#define se second
#define MOD(n,k) ( ( ((n) % (k)) + (k) ) % (k))
#define forn(i,n) for (int i = 0; i < n; i++)
#define forr(i,a,b) for (int i = a; i <= b; i++)
#define all(v) v.begin(), v.end()
#define pb(x) push_back(x)
 
using namespace std;
 
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;

const int MX = 1000005;
int st[4 * MX], lz[4 * MX];

void push (int i, int j, int pos) {
	if (i < j) {
		lz[pos * 2] += lz[pos];
		lz[pos * 2 + 1] += lz[pos];
	}

	st[pos] += lz[pos];
	lz[pos] = 0;
}

void update (int i, int j, int pos, int a, int b, int k) {
	push(i, j, pos);

	if (j < a || b < i) return;
	
	if (a <= i && j <= b) {
		lz[pos] = k;
		push(i, j, pos);
		return;
	}

	int m = (i + j) / 2;
	update(i, m, pos * 2, a, b, k);
	update(m + 1, j, pos * 2 + 1, a, b, k);

	st[pos] = max(st[pos * 2], st[pos * 2 + 1]);
}

int n;
string s;

int solve (int mx) {
	memset(st, 0, sizeof(st));
	memset(lz, 0, sizeof(lz));

	int acu = 0, mn = 0;

	forn (i, n) {
		char c = s[i];

		if (c == '1') acu++;
		else acu--;
		update(1, n, 1, i + 1, i + 1, acu);
	}

	acu = 0;
	forn (i, n) {
		char c = s[i];

		if (c == '1') acu++;
		else if (c == '0') acu--;
		else {
			update(1, n, 1, i + 1, n, 2);
			
			if (st[1] <= mx) {
				acu++;
			} else {
				update(1, n, 1, i + 1, n, -2);
				acu--;
			}
		}

		mn = min(mn, acu);
	}

	return mn;
}

int main () {
	ios_base::sync_with_stdio(0); cin.tie(0);
	
	cin >> s;
	n = s.size();

	int acu = 0, mx = 0;

	for (int c : s) {
		if (c == '1') acu++;
		else acu--;
		mx = max(mx, acu);
	}

	cout << min(mx - solve(mx), mx + 1 - solve(mx + 1)) << endl;

	return 0;
}
