/*
 * Since g++10 is released, some characters is not valid inside #if 0 :(
 * So, why not using clang++? :D

 * Date:
  echo -n '  ' && date +%Y.%m.%d # Just Run this (Type !!sh in Vim).

 * Solution:
  To be updated after "Accept".

 * Digression:

 * CopyRight:
          ▁▃▄▄▄▃▃▃▃▄▶
        ▗▇▀▔    ▔▔▔▔
       ▄▛   ▃▅━━■▄▂
      ▟▊   ▐▘     ▀▙
     ▟▜▌   ▐▖   ▋  ▐▍
    ▟▘ ▜   ▝▀▇▆●▘  ▐▌
  ▗▟▘   ▜▃       ▁▅▛
  ▔▀▼▅▄▃▃██▅▄▄▄▅■▀▔
        ▔▔▔▔▔▔
 */

#include <cstdio>
#include <cstring>
#include <algorithm>
#define debug(...) fprintf(stderr, __VA_ARGS__)

typedef long long ll;

const int maxn = 1000005;
char s[maxn];
int pre[maxn];
int smax[maxn];

int f (int n, int lim) {
	int add = 0, p = 0, res = 0;
	for (int i = 1; i <= n; i ++) {
		p += (s[i] == '1' ? 1 : -1);
		if (s[i] == '?' and add + smax[i] + 2 <= lim)
			add += 2, p += 2;
		res = std::min(res, p);
	}
	return res;
}

int main () {
	scanf("%s", s + 1);
	int n = int(strlen(s + 1));

	int max = 0;
	for (int i = 1; i <= n; i ++) {
		pre[i] = pre[i - 1] + (s[i] == '1' ? 1 : -1);
		max = std::max(max, pre[i]);
	}

	smax[n] = pre[n];
	for (int i = n; i; i --)
		smax[i - 1] = std::max(smax[i], pre[i - 1]);

	printf("%d\n", std::min(max - f(n, max), max + 1 - f(n, max + 1)));
}
