#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#pragma GCC optimize("unroll-loops")
//#pragma warning(disable : 4996)

#ifdef _MSC_VER
#include <intrin.h>
#define __builtin_popcount __popcnt
#define __builtin_popcountll __popcnt64
#endif

#include <limits.h>
#include <math.h>
#include <time.h>

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
#define FORR(i, m, n) for (int i = m - 1; i >= n; --i)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define REVERSE(v, n) reverse(v, v + n);
#define VREVERSE(v) reverse(v.begin(), v.end())
#define ll long long
#define print(x) cout << (x) << '\n'
#define pe(x) cout << (x) << " "
#define DEBUG(x) cout << #x << ": " << x << endl
#define lb(v, n) lower_bound(v.begin(), v.end(), (n))
#define ub(v, n) upper_bound(v.begin(), v.end(), (n))
//#define int long long
#define double long double
#define all(x) (x).begin(), (x).end()
#define print_space(v) REP(i, v.size()) cout << v[i] << ((i == v.size() - 1) ? "\n" : " ")
template <typename T1, typename T2> inline void chmin(T1& a, T2 b) {
	if (a > b) a = b;
}
template <typename T1, typename T2> inline void chmax(T1& a, T2 b) {
	if (a < b) a = b;
}
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef array<int, 3> arr3;
std::random_device rd;
std::mt19937 mt(rd());
constexpr ll MOD = 1e9 + 7;
constexpr int MAX = 500050;
const double pi = acos(-1);
constexpr double EPS = 1e-10;
constexpr ll LINF = 1e18 + 1;
constexpr int INF = 1e9 + 1;
string S;
string T;
int dpr[2][1000010], dpl[2][1000010];
int N;
bool check(int x) {
	S = T;
	int ret1 = 0;
	REP(j, 2) {
		REP(i, N + 2) {
			dpr[j][i] = 0;
			dpl[j][i] = 0;
		}
	}
	REPR(i, N) {
		int res1 = dpr[1][i + 1] + ((S[i] == '1') ? 1 : -1);
		dpr[1][i] = max(0, res1);
		int res0 = dpr[0][i + 1] + ((S[i] == '0') ? 1 : -1);
		dpr[0][i] = max(0, res0);
		//DEBUG(i);
		//DEBUG(res0);
		//DEBUG(res1);
		ret1 = max({ ret1,res0,res1 });
	}
	if (ret1 > x)return false;
	REP(i, N) {
		if (S[i] == '?') {
			//0にする
			int res0 = dpl[0][i] + dpr[0][i + 1];
			int res1 = dpl[1][i] + dpr[1][i + 1];
			if (res0 < res1)S[i] = '0';
			else S[i] = '1';

		}
		int res0 = dpl[0][i] + ((S[i] == '0') ? 1 : -1);
		int res1 = dpl[1][i] + ((S[i] == '1') ? 1 : -1);
		dpl[0][i + 1] = max(0, res0);
		dpl[1][i + 1] = max(0, res1);
		ret1 = max({ ret1,res0,res1 });
	}
	S = T;
	VREVERSE(S);

	int ret2 = 0;
	REP(j, 2) {
		REP(i, N + 2) {
			dpr[j][i] = 0;
			dpl[j][i] = 0;
		}
	}
	REPR(i, N) {
		int res1 = dpr[1][i + 1] + ((S[i] == '1') ? 1 : -1);
		dpr[1][i] = max(0, res1);
		int res0 = dpr[0][i + 1] + ((S[i] == '0') ? 1 : -1);
		dpr[0][i] = max(0, res0);
		//DEBUG(i);
		//DEBUG(res0);
		//DEBUG(res1);
		ret2 = max({ ret2,res0,res1 });
	}
	if (ret2 > x)return false;
	REP(i, N) {
		if (S[i] == '?') {
			//0にする
			int res0 = dpl[0][i] + dpr[0][i + 1];
			int res1 = dpl[1][i] + dpr[1][i + 1];
			if (res0 < res1)S[i] = '0';
			else S[i] = '1';

		}
		int res0 = dpl[0][i] + ((S[i] == '0') ? 1 : -1);
		int res1 = dpl[1][i] + ((S[i] == '1') ? 1 : -1);
		dpl[0][i + 1] = max(0, res0);
		dpl[1][i + 1] = max(0, res1);
		ret2 = max({ ret2,res0,res1 });
	}
	int ret = min(ret1, ret2);
	return ret <= x;
}
bool check2(int x) {
	S = T;
	int ret1 = 0;
	REP(j, 2) {
		REP(i, N + 2) {
			dpr[j][i] = 0;
			dpl[j][i] = 0;
		}
	}
	REPR(i, N) {
		int res1 = dpr[1][i + 1] + ((S[i] == '1') ? 1 : -1);
		dpr[1][i] = max(0, res1);
		int res0 = dpr[0][i + 1] + ((S[i] == '0') ? 1 : -1);
		dpr[0][i] = max(0, res0);
		//DEBUG(i);
		//DEBUG(res0);
		//DEBUG(res1);
		ret1 = max({ ret1,res0,res1 });
	}
	if (ret1 > x)return false;
	REP(i, N) {
		if (S[i] == '?') {
			//0にする
			int res0 = dpl[0][i] + dpr[0][i + 1];
			int res1 = dpl[1][i] + dpr[1][i + 1];
			if (res0 <= res1)S[i] = '0';
			else S[i] = '1';

		}
		int res0 = dpl[0][i] + ((S[i] == '0') ? 1 : -1);
		int res1 = dpl[1][i] + ((S[i] == '1') ? 1 : -1);
		dpl[0][i + 1] = max(0, res0);
		dpl[1][i + 1] = max(0, res1);
		ret1 = max({ ret1,res0,res1 });
	}
	S = T;
	VREVERSE(S);

	int ret2 = 0;
	REP(j, 2) {
		REP(i, N + 2) {
			dpr[j][i] = 0;
			dpl[j][i] = 0;
		}
	}
	REPR(i, N) {
		int res1 = dpr[1][i + 1] + ((S[i] == '1') ? 1 : -1);
		dpr[1][i] = max(0, res1);
		int res0 = dpr[0][i + 1] + ((S[i] == '0') ? 1 : -1);
		dpr[0][i] = max(0, res0);
		//DEBUG(i);
		//DEBUG(res0);
		//DEBUG(res1);
		ret2 = max({ ret2,res0,res1 });
	}
	if (ret2 > x)return false;
	REP(i, N) {
		if (S[i] == '?') {
			//0にする
			int res0 = dpl[0][i] + dpr[0][i + 1];
			int res1 = dpl[1][i] + dpr[1][i + 1];
			if (res0 <= res1)S[i] = '0';
			else S[i] = '1';

		}
		int res0 = dpl[0][i] + ((S[i] == '0') ? 1 : -1);
		int res1 = dpl[1][i] + ((S[i] == '1') ? 1 : -1);
		dpl[0][i + 1] = max(0, res0);
		dpl[1][i + 1] = max(0, res1);
		ret2 = max({ ret2,res0,res1 });
	}
	int ret = min(ret1, ret2);
	return ret <= x;
}
bool check3(int x) {
	S = T;
	//if (x == 1)print(S);
	int ret1 = 0;
	REP(j, 2) {
		REP(i, N + 2) {
			dpr[j][i] = 0;
			dpl[j][i] = 0;
		}
	}
	REPR(i, N) {
		int res1 = dpr[1][i + 1] + ((S[i] == '1') ? 1 : -1);
		dpr[1][i] = max(0, res1);
		int res0 = dpr[0][i + 1] + ((S[i] == '0') ? 1 : -1);
		dpr[0][i] = max(0, res0);
		//DEBUG(i);
		//DEBUG(res0);
		//DEBUG(res1);
		ret1 = max({ ret1,res0,res1 });
	}
	if (ret1 > x) {
		return false;
		if (x == 1) {
			//print("no");
		}
	}
	REP(i, N) {
		if (S[i] == '?') {
			//0にする
			int res0 = dpl[0][i] + dpr[0][i + 1];
			int res1 = dpl[1][i] + dpr[1][i + 1];
			if (res0 < res1)S[i] = '0';
			else if (res0 == res1) {
				if (mt() % 2 == 0)S[i] = '0';
				else S[i] = '1';
			}
			else S[i] = '1';

		}
		int res0 = dpl[0][i] + ((S[i] == '0') ? 1 : -1);
		int res1 = dpl[1][i] + ((S[i] == '1') ? 1 : -1);
		dpl[0][i + 1] = max(0, res0);
		dpl[1][i + 1] = max(0, res1);
		ret1 = max({ ret1,res0,res1 });
	}
	S = T;
	VREVERSE(S);

	int ret2 = 0;
	REP(j, 2) {
		REP(i, N + 2) {
			dpr[j][i] = 0;
			dpl[j][i] = 0;
		}
	}
	REPR(i, N) {
		int res1 = dpr[1][i + 1] + ((S[i] == '1') ? 1 : -1);
		dpr[1][i] = max(0, res1);
		int res0 = dpr[0][i + 1] + ((S[i] == '0') ? 1 : -1);
		dpr[0][i] = max(0, res0);
		//DEBUG(i);
		//DEBUG(res0);
		//DEBUG(res1);
		ret2 = max({ ret2,res0,res1 });
	}
	if (ret2 > x) {
		return false;
		if (x == 1) {
			//print("no");
		}
	}
	REP(i, N) {
		if (S[i] == '?') {
			//0にする
			int res0 = dpl[0][i] + dpr[0][i + 1];
			int res1 = dpl[1][i] + dpr[1][i + 1];
			if (res0 < res1)S[i] = '0';
			else if (res0 == res1) {
				if (mt() % 2 == 0)S[i] = '0';
				else S[i] = '1';
			}
			else S[i] = '1';
		}
		int res0 = dpl[0][i] + ((S[i] == '0') ? 1 : -1);
		int res1 = dpl[1][i] + ((S[i] == '1') ? 1 : -1);
		dpl[0][i + 1] = max(0, res0);
		dpl[1][i + 1] = max(0, res1);
		ret2 = max({ ret2,res0,res1 });
	}
	int ret = min(ret1, ret2);
	return ret <= x;
}

int calc(string S) {
	int N = S.size();
	int ans = 0;
	REP(l, N) {
		FOR(r, l + 1, N+1) {
			int c0 = 0, c1 = 0;
			FOR(i, l, r) {
				if (S[i] == '0')c0++;
				else c1++;
			}
			chmax(ans, abs(c0 - c1));
		}
	}
	return ans;
}

int naive(string S) {
	int cnt = 0;
	int N = S.size();
	int mn = S.size();
	vector<int>pos;
	REP(i, N)if (S[i] == '?') {
		pos.push_back(i);
		cnt++;
	}
	REP(bit, 1 << cnt) {
		string s = S;
		REP(i, cnt) {
			if (bit&(1 << i)) {
				s[pos[i]] = '1';
			}
			else s[pos[i]] = '0';
		}
		//DEBUG(s);
		//DEBUG(calc(s));
		mn = min(mn, calc(s));
	}
	//if (cnt == 0)mn = calc(S);
	return mn;
}

int solve_(string s) {
	//mt.seed((int)(time(0)));

	S = s;
	T = S;
	N = S.size();
	int ok = N, ng = 0;
	while (abs(ok - ng) > 1) {
		int mid = (ok + ng) / 2;
		//DEBUG(mid);
		//print(mid);
		if (check(mid)||check2(mid)||check3(mid)||check3(mid)||check3(mid)||check3(mid)||check3(mid)||check3(mid))ok = mid;
		else ng = mid;
	}
	return ok;
}

bool test() {
	int n = mt() % 10 + 1;
	string s;

	REP(i, n) {
		int p = mt() % 3;
		if (p == 0)s += '0';
		else if (p == 1)s += '1';
		else s += '?';
	}
	print(s);
	int ans_naive = naive(s);
	int ans_my = solve_(s);
	if (ans_naive != ans_my) {
		//print(s);
		DEBUG(ans_naive);
		DEBUG(ans_my);
		exit(0);
		return false;
	}
	print("ok");
	return true;
}


void solve() {
	int c = (unsigned)time(NULL) % 100;
	REP(_, c)mt();
	cin >> S;
	//if (S.size() <= 20) {
	//	print(naive(S));
	//	return;
	//}
	T = S;
	N = S.size();
	int ok = N, ng = 0;
	while (abs(ok - ng) > 1) {
		int mid = (ok + ng)/2;
		//DEBUG(mid);
		if (check(mid) || check2(mid)||check3(mid)||check3(mid)||check3(mid)||check3(mid)||check3(mid))ok = mid;
		else ng = mid;
	}
	print(ok);
}



signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	mt.seed((int)(time(0)));
	//REP(_, 10000)test();
	//int q;
	//cin >> q;
	//while (q--)
	solve();
}



Measured memory_bytes: 20932