#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

const int MAXN = 1000010;

string s;

int prefix[MAXN];
int capacity[MAXN];

int cal(int maxPrefix) {
	int maxNumber = maxPrefix - prefix[s.size()];
	for (int i = s.size(); i > 0; i--) {
		maxNumber = min(maxNumber, maxPrefix - prefix[i]);
		capacity[i] = maxNumber / 2;
	}
	int curPrefix = 0;
	int tempMaxPrefix = 0;
	int tempMinPrefix = 0;
	int counts = 0;
	for (int i = 0; i < s.size(); i++) {
		if (s[i] == '1') {
			curPrefix++;
		}
		else if (s[i] == '0') {
			curPrefix--;
		}
		else {
			// ?
			if (capacity[i + 1] > counts) {
				curPrefix++;
				counts++;
			}
			else {
				curPrefix--;
			}
		}
		tempMaxPrefix = max(tempMaxPrefix, curPrefix);
		tempMinPrefix = min(tempMinPrefix, curPrefix);
	}
	return tempMaxPrefix - tempMinPrefix;
}

int main() {
	cin >> s;
	prefix[0] = 0;
	int maxPrefix = 0;
	for (int i = 0; i < s.size(); i++) {
		if (s[i] == '1') {
			prefix[i + 1] = prefix[i] + 1;
		}
		else {
			prefix[i + 1] = prefix[i] - 1;
		}
		maxPrefix = max(maxPrefix, prefix[i + 1]);
	}
	cout << min(cal(maxPrefix), cal(maxPrefix + 1)) << endl;
}