#include <bits/stdc++.h>
using namespace std;
const int TOUR = 1 << 20;
const int MAX = TOUR;
char s[MAX];
struct tour {
  int mn, mx, prop;
} t[2 * TOUR];
void propagiraj(int pos) {
  for (int i = (2 * pos); i < (2 * pos + 2); i++) {
    t[i].mn += t[pos].prop;
    t[i].mx += t[pos].prop;
    t[i].prop += t[pos].prop;
  }
  t[pos].prop = 0;
}
void update(int pos) {
  t[pos].mn = min(t[2 * pos].mn, t[2 * pos + 1].mn);
  t[pos].mx = max(t[2 * pos].mx, t[2 * pos + 1].mx);
}
int get_first_min(int pos, int lo, int hi, int val) {
  if (lo + 1 == hi) return lo;
  propagiraj(pos);
  if (t[2 * pos].mn <= val)
    return get_first_min(2 * pos, lo, (lo + hi) / 2, val);
  return get_first_min(2 * pos + 1, (lo + hi) / 2, hi, val);
}
void increase(int pos, int lo, int hi, int begin, int end, int val) {
  if (lo >= end || hi <= begin) return;
  if (lo >= begin && hi <= end) {
    t[pos].mn += val;
    t[pos].mx += val;
    t[pos].prop += val;
    return;
  }
  propagiraj(pos);
  increase(2 * pos + 0, lo, (lo + hi) / 2, begin, end, val);
  increase(2 * pos + 1, (lo + hi) / 2, hi, begin, end, val);
  update(pos);
}
int n;
int pref[MAX];
set<int> S;
void init() {
  n = (int)strlen(s);
  for (int i = (0); i < (n); i++)
    if (s[i] == '?') S.insert(i + 1);
  for (int i = (0); i < (n); i++)
    pref[i + 1] = pref[i] + (s[i] == '1' ? 1 : -1);
  for (int i = (0); i < (n + 1); i++) t[TOUR + i].mn = t[TOUR + i].mx = pref[i];
  for (int i = (n + 1); i < (TOUR); i++) {
    t[TOUR + i].mn = 1e9;
    t[TOUR + i].mx = -1e9;
  }
  for (int i = TOUR - 1; i; i--) update(i);
}
int main() {
  scanf(" %s", s);
  init();
  int sol = -t[1].mn + t[1].mx;
  for (;;) {
    int pos = get_first_min(1, 0, TOUR, t[1].mn + 1);
    auto its = S.lower_bound(pos);
    if (its == S.end() || *its > pos) {
      if (its != S.begin()) its--;
    }
    if (its == S.end() || *its > pos) {
      pos = get_first_min(1, 0, TOUR, t[1].mn);
      its = S.lower_bound(pos);
      if (its == S.end() || *its > pos) {
        if (its == S.begin()) break;
      }
    }
    increase(1, 0, TOUR, *its, TOUR, 2);
    sol = min(sol, -t[1].mn + t[1].mx);
    S.erase(its);
  }
  printf("%d\n", sol);
  return 0;
}
