#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ll Z = 0;
  string s;
  cin >> s;
  ll rui = 0;
  ll a = 0;
  ll f = 0;
  ll s1 = s.size();
  ll b[s1 + 1];
  ll c[s1 + 1];
  for (ll i = 0; i < s1; i++) {
    if (s.at(i) == '1') {
      a++;
    } else {
      a--;
    }
    b[i] = a;
    c[i] = a;
    Z = max(Z, a);
  }
  for (ll i = s1 - 2; i >= 0; i--) {
    b[i] = max(b[i], b[i + 1]);
  }
  for (ll i = 0; i < s1; i++) {
    if (s.at(i) == '1') {
      rui++;
    } else if (s.at(i) == '0') {
      rui--;
    } else {
      if (rui - c[i - 1] + b[i] + 2 <= Z) {
        rui++;
      } else {
        rui--;
      }
    }
    f = min(f, rui);
  }
  ll Z1 = Z + 1;
  ll f1 = 0;
  rui = 0;
  for (ll i = 0; i < s1; i++) {
    if (s.at(i) == '1') {
      rui++;
    } else if (s.at(i) == '0') {
      rui--;
    } else {
      if (rui + b[i] - c[i - 1] + 2 <= Z1) {
        rui++;
      } else {
        rui--;
      }
    }
    f1 = min(f1, rui);
  }
  ll ans = min(Z - f, Z1 - f1);
  cout << ans << endl;
}
