#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
#ifdef DEBUG
#define display(x) cerr << #x << " = " << (x) << endl;
#define displaya(a, st, n)\
	{cerr << #a << " = {";\
	for(int qwq = (st); qwq <= (n); ++qwq) {\
		if(qwq == (st)) cerr << ((a)[qwq]);\
		else cerr << ", " << ((a)[qwq]);\
	} cerr << "}" << endl;}
#define displayv(v) displaya(v, 0, (int)(v).size() - 1)
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define display(x) ;
#define displaya(a, st, n) ;
#define displayv(v) ;
#define eprintf(...) if(0) fprintf(stderr, "...")
#endif
template<typename T> bool chmin(T &a, const T &b) { return a > b ? a = b, true : false; }
template<typename T> bool chmax(T &a, const T &b) { return a < b ? a = b, true : false; }
template<typename A, typename B>
ostream& operator << (ostream& out, const pair<A, B> &p) {
	return out << '(' << p.first << ", " << p.second << ')';
}

const int maxN = 1000000 + 5;
int n, s[maxN];
char a[maxN];

int work(int low) {
	s[0] = 0;
	for(int i = 1; i <= n; ++i) s[i] = s[i - 1] + 1 - 2 * int(a[i] == '0');
	
	for(int i = n, pf = s[n]; i >= 1; --i) {
		chmin(pf, s[i]);
		s[i] = pf - s[i - 1];
	}
	displaya(s, 0, n);
	
	int now = 0, mx = 0;
	for(int i = 1; i <= n; ++i) {
		if(a[i] == '0') now--;
		else if(a[i] == '1') now++;
		else {
			if(now + s[i] - 2 >= low) now--;
			else now++;
		}
		chmax(mx, now);
		display(now);
	}
	return mx - low;
}

int main() {
#ifndef LOCAL
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
#endif
	cin >> (a + 1);
	n = strlen(a + 1);
	
	s[0] = 0;
	for(int i = 1; i <= n; ++i) s[i] = s[i - 1] + 1 - 2 * int(a[i] == '0');
	
	int mn = *min_element(s, s + n + 1);
	int a1 = work(mn);
	int a2 = work(mn - 1);
	eprintf("a1 = %d, a2 = %d\n", a1, a2);
	cout << min(a1, a2) << endl;
	
	return 0;
}
