#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
int main() {
  string S;
  cin >> S;
  ll N = S.size();
  vector<ll> sums;
  ll x = 0;
  for (ll i = 0; i < (ll)N; i++) {
    char c = S[i];
    if (c == '1') {
      x += 1;
    } else {
      x -= 1;
    }
    sums.push_back(x);
  }
  vector<ll> maxSums(N + 1, -1);
  ll maxSum = sums[N - 1];
  for (ll i = (ll)(N - 1); i >= 0; i--) {
    maxSum = max(maxSum, sums[i]);
    maxSums[i] = maxSum;
  }
  ll limit1 = max(maxSum, (ll)0);
  ll limit2 = max(maxSum + 1, (ll)0);
  string S1(S);
  string S2(S);
  ll cnt = 0;
  for (ll i = 0; i < (ll)N; i++) {
    if (S1[i] != '?') continue;
    ll cur = maxSums[i] + cnt * 2;
    if (limit1 >= cur + 2) {
      cnt += 1;
      S1[i] = '1';
    } else {
      S1[i] = '0';
    }
  }
  cnt = 0;
  for (ll i = 0; i < (ll)N; i++) {
    if (S2[i] != '?') continue;
    ll cur = maxSums[i] + cnt * 2;
    if (limit2 >= cur + 2) {
      cnt += 1;
      S2[i] = '1';
    } else {
      S2[i] = '0';
    }
  }
  ll min1 = 0;
  ll max1 = 0;
  x = 0;
  for (ll i = 0; i < (ll)N; i++) {
    char c = S1[i];
    if (c == '1') {
      x += 1;
    } else {
      x -= 1;
    }
    min1 = min(min1, x);
    max1 = max(max1, x);
  }
  ll min2 = 0;
  ll max2 = 0;
  x = 0;
  for (ll i = 0; i < (ll)N; i++) {
    char c = S2[i];
    if (c == '1') {
      x += 1;
    } else {
      x -= 1;
    }
    min2 = min(min2, x);
    max1 = max(max1, x);
  }
  ll ans = min(max1 - min1, max2 - min2);
  cout << ans << endl;
  return 0;
}
