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

typedef long long Int;
typedef pair<int,int> PII;
typedef vector<int> VInt;
 
#define FOR(i, a, b) for(i = (a); i < (b); ++i)
#define RFOR(i, a, b) for(i = (a) - 1; i >= (b); --i)
#define EACH(it, a) for(auto it = (a).begin(); it != (a).end(); ++it)
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define SIZE(a) int((a).size())
#define ALL(a) (a).begin(),(a).end()
#define MP make_pair

int f(VInt& s, vector<PII>& v, int mn)
{
	int pos = 0;
	int cnt = 0;
	int mx = 0;
	int i;
	FOR(i, 0, SIZE(s))
	{
		while(pos != SIZE(v) && v[pos].second < i) ++pos;
		if(S[i] == '?' && pos != SIZE(v)) 
		{
			int val = v[pos].first;
			int ind = v[pos].second;
			if(val - 2*cnt > mn + 1) ++cnt;
		}
		int t = s[i] - 2*cnt;
		mx = max(mx, t);
	}
}

void SolveTest(int test)
{
	string S;
	cin >> S;
	int i, j;
	int sum = 0;
	vector<PII> v;
	VInt s(SIZE(S));
	FOR(i, 0, SIZE(S))
	{
		sum += S[i] == '0' ? -1 : 1;
		s[i] = sum;
		v.push_back(PII(sum, i));
	}
	sort(ALL(v));
	int mn[] = { v[0].first - 1, v[0].first };
	int res = min(f(s, v, mn[0]) - mn[0], f(s, v, mn[1]) - mn[1]);
	cout << res << endl;
}

int main()
{
	int T, t;
	T = 1;
	FOR(t, 0, T) SolveTest(t);
	return 0;
};