#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <functional>
#include <vector>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <cassert>
#define InfL 2000000000
#define InfLL 4000000000000000000LL
#define mod 1000000007
#define rep(i,n) for(int i=0;i<n;i++)
#define rrep(i,n) for(int i=(n-1);i>=0;i--)
using namespace std;
typedef long long ll;
typedef double db;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<bool> vb;
typedef vector<db> vd;

int main() {
	string S;
	cin >> S;
	int N = S.length();
	vi Ssum(N + 1, 0);
	rep(i, N) {
		Ssum[i + 1] = Ssum[i];
		if (S[i] == '1')
			Ssum[i + 1]++;
		else
			Ssum[i + 1]--;
	}
	vi Ssummax(N + 1);
	Ssummax[N] = Ssum[N];
	rrep(i, N)
		Ssummax[i] = max(Ssummax[i + 1], Ssum[i]);
	int Ssummax_all = Ssummax[0];
	int ans = InfL;
	for (int Ssummax_all_new = Ssummax_all; Ssummax_all_new <= Ssummax_all + 1; Ssummax_all_new++) {
		int Ssummin_all_new = Ssum[0];
		int plus1_count = 0;
		rep(i, N) {
			int Ssumtmp = Ssum[i + 1] + plus1_count * 2;
			if (S[i] == '?') {
				if (Ssummax[i + 1] + 2 + plus1_count * 2 <= Ssummax_all_new) {
					// -1 -> +1
					plus1_count++;
					Ssumtmp += 2;
				}
			}
			Ssummin_all_new = min(Ssummin_all_new, Ssumtmp);
		}
		int anstmp = Ssummax_all_new - Ssummin_all_new;
		ans = min(ans, anstmp);
	}
	cout << ans << endl;
	return 0;
}