#include <bits/stdc++.h>
using namespace std;
template <typename T>
T sqr(T x) {
  return x * x;
}
template <typename T>
T abs(T x) {
  return x < 0 ? -x : x;
}
template <typename T>
T gcd(T a, T b) {
  return b ? gcd(b, a % b) : a;
}
template <typename T>
bool chmin(T& x, const T& y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}
template <typename T>
bool chmax(T& x, const T& y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}
int main(int, char**) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  string s;
  while (cin >> s) {
    int n = s.size();
    vector<int> f(n + 1, 0);
    vector<int> g(n + 1, 0);
    for (int i = n - 1; i >= 0; --i) {
      f[i] = max(0, f[i + 1] + 2 * (s[i] == '0') - 1);
      g[i] = max(0, g[i + 1] + 2 * (s[i] == '1') - 1);
    }
    int x, y;
    x = y = 0;
    int ans = 0;
    for (int i = 0; i < n; ++i) {
      if (s[i] == '?') {
        if (x + f[i + 1] >= y + g[i + 1]) {
          s[i] = '1';
        } else {
          s[i] = '0';
        }
      }
      x = max(0, x + 2 * (s[i] == '0') - 1);
      y = max(0, y + 2 * (s[i] == '1') - 1);
      ans = max(ans, max(x, y));
    }
    cout << ans << "\n";
  }
  cerr << "Time execute: " << clock() / (double)CLOCKS_PER_SEC << " sec"
       << endl;
  return 0;
}
