#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
int _w;
char str[N];
int n, l, r, mid, res, L, R;
int main(void) {
  _w = scanf("%s", str + 1);
  n = strlen(str + 1);
  l = 0, r = n, res = -1;
  bool flag, _f;
  while (l <= r) {
    mid = (l + r) >> 1;
    flag = 0;
    for (int _ = 0; _ < 2; ++_) {
      L = _, R = mid, _f = 1;
      if ((_ ^ R) & 1) --R;
      for (int i = 1; i <= n; ++i) {
        if (str[i] == '1') ++L, ++R;
        if (str[i] == '0') --L, --R;
        if (str[i] == '?') --L, ++R;
        if (L < 0) L += 2;
        if (R > mid) R -= 2;
        if (L > R) {
          _f = 0;
          break;
        }
      }
      flag |= _f;
    }
    if (flag)
      res = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  cout << res;
  return 0;
}
