#include <bits/stdc++.h>
using namespace std;
typedef long long i64;
const int MAX = 1e6 + 10;
const int MOD = 1e9 + 7;
const int oo = 1e9;

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	#define endl '\n'
	string s; cin >> s;
	int n = s.size();
	int b = 1, e = n;
	while(b < e){
		int m = (b + e) / 2;
		bool pos = true;
		int rd = 0, ru = m;
		int phase = 1;
		for(int i = 0; i < n; i++){
			if(s[i] == '0'){
				rd--; ru--;
			}
			else if(s[i] == '1'){
				rd++, ru++;
			}
			else{
				rd--; ru++;
			}
			if(rd < 0)rd += phase;
			if(ru > m) ru -= phase;
			if(ru < rd){
				pos = false; break;
			}
			if(ru == rd && phase == 1)phase = 2;
		}
		if(pos) e = m;
		else b = m + 1;
	}
	cout << b << endl;

}
