#include <bits/stdc++.h>
using namespace std;
namespace Base {
const int inf = 0x3f3f3f3f, INF = 0x7fffffff;
const long long infll = 0x3f3f3f3f3f3f3f3fll, INFll = 0x7fffffffffffffffll;
template <typename T>
void read(T &x) {
  x = 0;
  int fh = 1;
  double num = 1.0;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') fh = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  if (ch == '.') {
    ch = getchar();
    while (isdigit(ch)) {
      num /= 10;
      x = x + num * (ch - '0');
      ch = getchar();
    }
  }
  x = x * fh;
}
template <typename T>
void chmax(T &x, T y) {
  x = x < y ? y : x;
}
template <typename T>
void chmin(T &x, T y) {
  x = x > y ? y : x;
}
}  // namespace Base
using namespace Base;
const int N = 1000100;
char s[N], n;
int solve() {
  int f, l, r, lx, ly, rx, ry;
  f = 0, l = 0, r = 0;
  lx = 0, ly = 0;
  rx = 0, ry = 0;
  for (int i = 1; i <= n; i++) {
    if (s[i] == '0') {
      l--;
      r--;
      if (l < 0) l += 2;
      lx = max(0, lx - 1);
      ly = ly + 1;
      rx = max(0, rx - 1);
      ry = ry + 1;
      if (r < 0) l = r = rx, f = rx + ry;
    }
    if (s[i] == '1') {
      l++, r++;
      if (r > f) r -= 2;
      lx = lx + 1;
      ly = max(0, ly - 1);
      rx = rx + 1;
      ry = max(0, ry - 1);
      if (l > f) l = r = lx, f = lx + ly;
    }
    if (s[i] == '?') {
      lx = max(0, lx - 1);
      ly = ly + 1;
      rx = rx + 1;
      ry = max(0, ry - 1);
      l--;
      if (l < 0) l += 2;
      r++;
      if (r > f) r -= 2;
    }
    if (l == lx) ly = min(ly, f - l);
    if (f - r == ry) rx = min(rx, r);
  }
  return f;
}
int main() {
  scanf("\n%s", s + 1);
  n = strlen(s + 1);
  int ans;
  if (s[1] == '?') {
    s[1] = '0';
    ans = solve();
    s[1] = '1';
    ans = min(ans, solve());
  } else
    ans = solve();
  cout << ans << endl;
  return 0;
}
