#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

bool check(string& S, int limit)
{
	int i;
	int a = 0, b = limit, step = 1;
	FOR(i, 0, SIZE(S))
	{
		if(S[i] == '0')
		{
			--a;
			--b;
		}
		else if(S[i] == '1')
		{
			++a;
			++b;
		}
		else
		{
			--a;
			++b;
		}
		if(a < 0) a += step;
		if(b > limit) b -= step;
		if(a > b) return false;
		if(a == b) step = 2;
	}
	return true;
}

void SolveTest(int test)
{
	string S;
	cin >> S;
	int mn = 0, mx = SIZE(S);
	while(mx - mn > 1)
	{
		int md = (mx + mn)/2;
		if(check(S, md)) mx = md;
		else mn = md;
	}
	cout << mx << endl;
}

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