#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  string s;
  cin >> s;
  char f = s[0];
  int res = s.length();
  for (int i = 0; i < 2; ++i) {
    if (f != '?' && f - '0' != i) {
      continue;
    }
    s[0] = '0' + i;
    int k = 0, a = 0, b = 0;
    for (auto x : s) {
      if (x == '?') {
        if (k == 0) {
          assert(false);
        }
        a -= 1;
        b += 1;
        if (a == -1) {
          a = 1;
        }
        if (b == k + 1) {
          b = k - 1;
        }
      } else {
        int v = x == '0' ? +1 : -1;
        a += v;
        b += v;
        if (b < 0) {
          a = b = 0;
          k++;
        } else if (a < 0) {
          a = 1;
        }
        if (a > k) {
          k++;
        } else if (b > k) {
          b = k - 1;
        }
      }
      assert(a <= b);
      assert((a & 1) == (b & 1));
    }
    res = min(res, k);
  }
  cout << res << '\n';
  return 0;
}
