#include <bits/stdc++.h>
using namespace std;
template <class c>
struct rge {
  c b, e;
};
template <class c>
rge<c> range(c i, c j) {
  return rge<c>{i, j};
}
template <class c>
auto dud(c* x) -> decltype(cerr << *x, 0);
template <class c>
char dud(...);
struct debug {
  ~debug() { cerr << endl; }
  template <class c>
  typename enable_if<sizeof dud<c>(0) != 1, debug&>::type operator<<(c i) {
    cerr << boolalpha << i;
    return *this;
  }
  template <class c>
  typename enable_if<sizeof dud<c>(0) == 1, debug&>::type operator<<(c i) {
    return *this << range(begin(i), end(i));
  }
  template <class c, class b>
  debug& operator<<(pair<b, c> d) {
    return *this << "(" << d.first << ", " << d.second << ")";
  }
  template <class c>
  debug& operator<<(rge<c> d) {
    *this << "[";
    for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it;
    return *this << "]";
  }
};
int n;
char s[1234567];
int val[1234567];
void solve() {
  scanf("%s", s);
  n = strlen(s);
  val[0] = 0;
  for (int i = (0); i < (n); ++i) {
    if (s[i] == '0' || s[i] == '?') {
      val[i + 1] = val[i] - 1;
    } else {
      val[i + 1] = val[i] + 1;
    }
  }
  int maxi = val[0], mini = val[0];
  for (int i = (1); i < (n + 1); ++i) {
    maxi = max(maxi, val[i]);
    mini = min(mini, val[i]);
  }
  int ans = maxi - mini;
  int last_maxi = 0;
  int first_mini = n;
  for (int i = (0); i < (n + 1); ++i)
    if (val[i] == maxi) last_maxi = i;
  for (int i = (n); i >= (0); --i)
    if (val[i] == mini) first_mini = i;
  if (first_mini < last_maxi) {
    printf("%d\n", ans);
    return;
  }
  int max_after = mini;
  for (int i = (first_mini); i < (n + 1); ++i) {
    max_after = max(max_after, val[i]);
  }
  int do_gory = 0;
  for (int i = (last_maxi); i < (first_mini); ++i) {
    if (s[i] == '?') do_gory++;
  }
  ans = ans - min(do_gory, maxi - max_after);
  printf("%d\n", ans);
}
int main() {
  int z = 1;
  while (z--) {
    solve();
  }
}
