#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<long long, long long>;
signed main() {
  string s;
  cin >> s;
  vector<long long> d;
  d.push_back(0);
  long long now = 0;
  for (long long i = 0; i < (s.size()); ++i) {
    now += (s[i] == '1' ? -1 : 1);
    d.push_back(now);
  }
  long long mx = 0, mn = 0;
  for (long long di : d) {
    mx = max(mx, di);
    ;
    mn = min(mn, di);
    ;
  }
  long long mxind;
  for (long long i = 0; i < (d.size()); ++i) {
    if (d[i] == mx) {
      mxind = i;
      break;
    }
  }
  long long mnind;
  for (long long i = 0; i < (d.size()); ++i) {
    if (d[i] == mn) {
      mnind = i;
    }
  }
  if (mxind < mnind) {
    cout << mx - mn << endl;
    return 0;
  }
  priority_queue<P> q;
  for (long long i = mnind + 1; i < d.size(); i++) {
    q.emplace(-d[i], i);
  }
  long long cnt = 0;
  for (long long i = mnind; i < mxind; i++) {
    while (q.top().second <= i) q.pop();
    if (s[i] == '?' && !q.empty() && -q.top().first - (cnt + 1) * 2 >= mn) {
      mx -= 2;
      cnt += 1;
    }
  }
  cout << mx - mn << endl;
}
