#include <bits/stdc++.h>
#define ll long long
#define db long double
#define x first
#define y second
#define mp make_pair
#define pb push_back
#define all(a) a.begin(), a.end()

using namespace std;

bool check(string s, int x) {

	int l = 0, r = x, step = 1;

	for (int i = 0; i < s.size(); ++i) {
		if (l == r) step = 1;

		if (s[i] == '0') {
			l--, r--;
			if (l < 0) {
				l += step;
			}
			if (l > r) {
				return false;
			}
		}
		else if (s[i] == '1') {
			l++, r++;
			if (r > x) {
				r -= step;
			}
			if (l > r) {
				return false;
			}
		}
		else {
			if (step == 2 || l != r) {
				l--, r++;
				if (l < 0) {
					l += step;
				}
				if (r > x) {
					r -= step;
				}
				if (l > r) {
					return false;
				}
			}
			else {
				l--, r++;
				step = 2;
				if (l < 0) {
					l += step;
				}
				if (r > x) {
					r -= step;
				}
				if (l > r) {
					return false;
				}
			}
		}
	}

	return true;

}

int main(){
#ifdef LOCAL
	freopen("H_input.txt", "r", stdin);
	//freopen("H_output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	string s;
	cin >> s;

	int L = 0, R = 1e7;
	while (R-L>1) {
		int M = (L+R)/2;
		if (check(s, M)) R = M;
		else L = M;
	}

	cout << R;

}
