#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  string s;
  cin >> s;
  int n = (int)s.size();
  bool can_be_1 = false;
  {
    bool ok = true;
    for (int i = 0; i < n; i++) {
      if (i % 2 == 0 && s[i] == '1') {
        ok = false;
      }
      if (i % 2 == 1 && s[i] == '0') {
        ok = false;
      }
    }
    can_be_1 |= ok;
  }
  {
    bool ok = true;
    for (int i = 0; i < n; i++) {
      if (i % 2 == 0 && s[i] == '0') {
        ok = false;
      }
      if (i % 2 == 1 && s[i] == '1') {
        ok = false;
      }
    }
    can_be_1 |= ok;
  }
  int bound = (can_be_1 ? 1 : 2);
  vector<int> L0(n);
  vector<int> L1(n);
  vector<int> R0(n);
  vector<int> R1(n);
  int ans = -1;
  for (int iter = 0; iter < 50; iter++) {
    ans = bound;
    {
      int b = 0;
      for (int i = 0; i < n; i++) {
        L0[i] = b;
        if (s[i] == '0') {
          b += 1;
        } else {
          b = max(b - 1, 0);
        }
        ans = max(ans, b);
      }
    }
    {
      int b = 0;
      for (int i = 0; i < n; i++) {
        L1[i] = b;
        if (s[i] == '1') {
          b += 1;
        } else {
          b = max(b - 1, 0);
        }
        ans = max(ans, b);
      }
    }
    {
      int b = 0;
      for (int i = n - 1; i >= 0; i--) {
        R0[i] = b;
        if (s[i] == '0') {
          b += 1;
        } else {
          b = max(b - 1, 0);
        }
        ans = max(ans, b);
      }
    }
    {
      int b = 0;
      for (int i = n - 1; i >= 0; i--) {
        R1[i] = b;
        if (s[i] == '1') {
          b += 1;
        } else {
          b = max(b - 1, 0);
        }
        ans = max(ans, b);
      }
    }
    for (int i = 0; i < n; i++) {
      if (L0[i] == R1[i]) {
        ans = max(ans, L0[i] + 1);
      }
      if (L1[i] == R0[i]) {
        ans = max(ans, L1[i] + 1);
      }
    }
    for (int i = 0; i < n; i++) {
      if (s[i] == '?') {
        if (L0[i] == ans || R0[i] == ans) {
          s[i] = '1';
        }
        if (L1[i] == ans || R1[i] == ans) {
          s[i] = '0';
        }
      }
    }
  }
  cout << ans << '\n';
  return 0;
}
