#include <bits/stdc++.h>
using namespace std;
struct SEG {
  static const int maxn = 1 << 20;
  struct T {
    int sum, pre;
    T() : sum(0), pre(0) {}
    T(int sum, int pre) : sum(sum), pre(pre) {}
    T operator+(const T &rhs) const {
      return T(sum + rhs.sum, min(pre, sum + rhs.pre));
    }
  } t[maxn << 1];
  void upd(int x, int v) {
    t[x += maxn] = T(v, min(v, 0));
    while (x >>= 1) t[x] = t[x << 1] + t[x << 1 | 1];
  }
  T qry(int l, int r) {
    T L, R;
    for (l += maxn, r += maxn; l < r; l >>= 1, r >>= 1) {
      if (l & 1) L = L + t[l++];
      if (r & 1) R = t[--r] + R;
    }
    return L + R;
  }
} seg;
int n;
char s[1 << 20];
bool chk(int lim) {
  static int stk[1 << 20];
  int top = 0;
  int nw_diff = 0;
  for (int i = 0; i < (int)(n); ++i) {
    char c = s[i] == '?' ? '0' : s[i];
    if (s[i] == '?') stk[top++] = i;
    seg.upd(i, c == '0' ? 1 : -1);
    nw_diff += c == '0' ? 1 : -1;
    if (nw_diff - seg.qry(0, i + 1).pre > lim) {
      if (not top) return 0;
      --top;
      seg.upd(stk[top], -1);
      nw_diff -= 2;
    }
  }
  {
    int nw_diff = 0, mn_diff = 0;
    for (int i = 0; i < (int)(n); ++i) {
      int x = seg.qry(i, i + 1).sum;
      nw_diff -= x;
      mn_diff = min(nw_diff, mn_diff);
      if (nw_diff - mn_diff > lim) return 0;
    }
  }
  return 1;
}
int main() {
  scanf("%s", s);
  n = strlen(s);
  int lb = 1, rb = n;
  while (lb < rb) {
    int md = (lb + rb) >> 1;
    if (chk(md))
      rb = md;
    else
      lb = md + 1;
  }
  printf("%d\n", lb);
  return 0;
}
