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

int main(){
	ios_base::sync_with_stdio(false), cin.tie(nullptr);
	string s;
	cin >> s;
	int n = (int)s.size();
	vector<int> left0(n+1, 0), left1(n+1, 0);
	{
		int min0 = 0;
		int min1 = 0;
		int cur0 = 0;
		int cur1 = 0;
		for(int i = 0; i < n; i++){
			if(s[i] == '0') cur0++;
			if(s[i] != '0') cur0--;
			if(s[i] == '1') cur1++;
			if(s[i] != '1') cur1--;
			min0 = min(min0, cur0);
			min1 = min(min1, cur1);
			left0[i+1] = cur0 - min0;
			left1[i+1] = cur1 - min1;
		}
	}
	int odd0 = 0, odd1 = 0, even0 = 0, even1 = 0;
	for(int i = 0; i <= n; i++){
		if(i & 1){
			odd0 = max(odd0, left0[i]);
			odd1 = max(odd1, left1[i]);
		} else {
			even0 = max(even0, left0[i]);
			even1 = max(even1, left1[i]);			
		}
	}
	int ans = 0;
	ans = max(ans, odd0);
	ans = max(ans, odd1);
	ans = max(ans, even0);
	ans = max(ans, even1);
	if(odd0 == even0) ans = max(ans, odd0+1);
	if(odd1 == even1) ans = max(ans, odd1+1);
	cout << ans << '\n';
}