#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = (a); i <= int(b); i++)
#define per(i, a, b) for (int i = (a); i >= int(b); i--)
using namespace std;

const int maxn = 1e6;
char s[maxn + 5];
int n, b[maxn + 5];

int solve(int low) {
	int x = 0, hi = 0;
	rep(i, 1, n) {
		if (s[i] != '?') {
			x += s[i] == '0' ? 1 : -1;
		} else {
			if (x - 1 + b[i + 1] >= low) x--;
			else x++;
		}
		hi = max(hi, x);
	}
	return hi - low;
}

int main() {
	scanf("%s", s + 1), n = strlen(s + 1);
	per(i, n, 1) b[i] = min(0, b[i + 1] + (s[i] != '1' ? 1 : -1));
	printf("%d\n", min(solve(b[1]), solve(b[1] - 1)));
	return 0;
}