#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll,ll> pll;
typedef vector<bool> vb;
const ll oo = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-9;
#define sz(c) ll((c).size())
#define all(c) begin(c), end(c)
#define FOR(i,a,b) for (ll i = (a); i < (b); i++)
#define FORD(i,a,b) for (ll i = (b)-1; i >= (a); i--)
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define xx first
#define yy second
#define TR(X) ({ if(1) cerr << "TR: " << (#X) << " = " << (X) << endl; })

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	
	string s; cin >> s;
	ll n = sz(s);

	ll kmin = 0, kmax = n+1;
	while (kmax-kmin > 1) {
		ll k = (kmin+kmax)/2;
		
		bool ok2 = false;
		FOR(m,0,2) {
			bool ok = true;
			ll lo = m, hi = k;
			while (hi%2 != m) hi--;
			
			if (lo > hi) ok = false;

			for (char c: s) {
				if (c == '0') lo++;
				else if (lo > 0) lo--;
				else lo++;

				if (c == '1') hi--;
				else if (hi < k) hi++;
				else hi--;
				
				if (lo > hi) ok = false;
			}
			if (ok) ok2 = true;
		}

		if (ok2) kmax = k; else kmin = k;
	}
	cout << kmax << endl;
}

