#include <bits/stdc++.h>
using namespace std;
signed main() {
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  string s;
  cin >> s;
  long long o = 0, z = 0, buffer = 0, ans = 1;
  for (long long i = 0; i < s.size(); i++) {
    if (s[i] == '1') o++;
    if (s[i] == '0') z++;
    if (s[i] == '?') {
      if (o > z) {
        z++;
        s[i] = 0;
      } else if (z > o) {
        o++;
        s[i] = 1;
      } else {
        buffer++;
      }
    }
    if (abs(o - z) - buffer < 0) {
      o = 0;
      z = 0;
      buffer = 0;
    }
    ans = max(ans, abs(o - z) - buffer);
  }
  o = 0;
  z = 0;
  buffer = 0;
  for (long long i = s.size() - 1; i > -1; i--) {
    if (s[i] == '1') o++;
    if (s[i] == '0') z++;
    if (s[i] == '?') {
      if (o > z) {
        z++;
        s[i] = 0;
      } else if (z > o) {
        o++;
        s[i] = 1;
      } else {
        buffer++;
      }
    }
    if (abs(o - z) - buffer < 0) {
      o = 0;
      z = 0;
      buffer = 0;
    }
    ans = max(ans, abs(o - z) - buffer);
  }
  o = 0;
  z = 0;
  buffer = 0;
  long long sum = 0;
  for (long long i = 0; i < s.size(); i++) {
    if (s[i] == '1') sum++;
    if (s[i] == '0') sum--;
    if (s[i] == '?') {
      buffer++;
    }
    if (sum < 0) {
      sum = 0;
      buffer = 0;
    }
    ans = max(ans, sum + buffer % 2);
  }
  sum = 0;
  for (long long i = 0; i < s.size(); i++) {
    if (s[i] == '1') sum--;
    if (s[i] == '0') sum++;
    if (s[i] == '?') {
      buffer++;
    }
    if (sum < 0) {
      sum = 0;
      buffer = 0;
    }
    ans = max(ans, sum + buffer % 2);
  }
  cout << ans;
}
