#include <bits/stdc++.h>

#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " : " << x << '\n'

using namespace std;

typedef long long ll;
typedef long double ld;
typedef string str;
typedef pair<ll, ll> pll;

const ll Mod = 1000000007LL;
const int N = 2e5 + 10;
const ll Inf = 2242545357980376863LL;
const ll Log = 30;

int main(){
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	str s;
	cin >> s;
	int n = s.size();
	int L = 0, R = n + 3;
	int mid;
	int lz, rz, lf, rf;
	while(L + 1 < R){
		mid = (L + R) >> 1;
		lz = 0, rz = mid - (mid & 1);
		lf = 1, rf = mid - (1 - (mid & 1));

		bool flz = true, flf = true;
		for(auto &c : s){
			if(c == '0'){
				lz --; rz--;
				lf --; rf--;
			}
			if(c == '1'){
				lz ++;
				rz ++;
				lf ++; rf++;
			}
			if(c == '?'){
				lz --;
				rz ++;
				lf --;
				rf ++;
			}
			swap(lz, lf); swap(rz, rf); swap(flz, flf);
			lz = max(lz, 0); lf = max(lf, 1);
			rz = min(rz, mid - (mid & 1));
			rf = min(rf, mid - (1 - (mid & 1)));
			if(lz > rz) flz = false;
			if(lf > rf) flf = false;
		}
		if(flz || flf) R = mid;
		else L = mid;
	}
	cout << R << '\n';
	return 0;
}
