#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 7, INF = 0x3f3f3f3f, sgn[] = {-1, 1};
char a[N];
int n, s[N], nxt[N];
int solve(int x, int cnt = 0) {
  int w = x;
  for (int i = 1; i <= n; i++) {
    if (a[i] == '?' && cnt + 2 + nxt[i] <= nxt[0]) cnt += 2;
    w = min(w, s[i] + cnt);
  }
  return x - w;
}
int main() {
  scanf("%s", a + 1), n = strlen(a + 1), nxt[n + 1] = -INF;
  for (int i = 1; i <= n; i++) s[i] = s[i - 1] + sgn[a[i] == '1'];
  for (int i = n; i >= 0; i--) nxt[i] = max(nxt[i + 1], s[i]);
  cout << min(solve(nxt[0]), solve(nxt[0] + 1));
  return 0;
}
