#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T>
using V = vector<T>;
template <class T, class U>
using P = pair<T, U>;
using vll = V<ll>;
using vvll = V<vll>;
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
const long long MOD = 1000000007;
const long long HIGHINF = (long long)1e18;
const int INF = (int)1e9;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string s;
  cin >> s;
  int n = s.size();
  V<int> pm(n + 1, 0);
  for (int i = 0; i < n; i++) {
    int tmp = 0;
    if (s[i] == '0')
      tmp = -1;
    else if (s[i] == '1')
      tmp = 1;
    pm[i + 1] = pm[i] + tmp;
  }
  V<int> postmax(n + 1, pm.back()), postmin(n + 1, pm.back());
  for (int i = n - 1; i >= 0; i--) {
    postmax[i] = max(postmax[i + 1], pm[i + 1]);
    postmin[i] = min(postmin[i + 1], pm[i + 1]);
  }
  int diff = 0;
  int premax = 0, premin = 0;
  int ans = max(abs(premax), abs(premin));
  int tmp = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '?') {
      int tmpmin = postmin[i + 1] + diff - 1;
      if (tmpmin > premin) {
        premin = tmpmin;
        int tmpmax = postmax[i + 1] + diff - 1;
        if (tmpmax > premax) premax = tmpmax;
        diff--;
        continue;
      }
      int tmpmax = postmax[i + 1] + diff + 1;
      if (tmpmax < premax) {
        premax = tmpmax;
        int tmpmin = postmin[i + 1] + diff + 1;
        if (tmpmin < premin) premin = tmpmin;
        diff++;
        continue;
      }
      if (abs(tmpmin) == abs(premin)) {
        int tmpmax = postmax[i + 1] + diff - 1;
        if (tmpmax > premax) premax = tmpmax;
        diff--;
      } else if (abs(tmpmax) == abs(premax)) {
        int tmpmin = postmin[i + 1] + diff + 1;
        if (tmpmin < premin) premin = tmpmin;
        diff++;
      } else {
        premax = tmpmax;
        int tmpmin = postmin[i + 1] + diff + 1;
        if (tmpmin < premin) premin = tmpmin;
        diff++;
      }
    } else if (s[i] == '0') {
      tmp--;
      chmin(premin, tmp);
    } else {
      tmp++;
      chmax(premax, tmp);
    }
  }
  if ((ll)premax * (ll)premin > 0)
    cout << abs(premax - premin) << '\n';
  else
    cout << abs(premax) + abs(premin) << '\n';
  return 0;
}
