#include <bits/stdc++.h>
using namespace std;
struct node {
  node *l, *r;
  int a, b;
  int add;
  int Min;
  int Max;
  int pos;
  node(int _a, int _b)
      : a(_a), b(_b), l(NULL), r(NULL), add(0), Min(0), Max(0), pos(0) {}
} * root;
int Min(node *n) { return n->add + n->Min; }
int Max(node *n) { return n->add + n->Max; }
void pull(node *n) {
  n->Min = min(Min(n->l), Min(n->r));
  if (Max(n->l) >= Max(n->r)) {
    n->pos = n->l->pos;
    n->Max = Max(n->l);
  } else {
    n->pos = n->r->pos;
    n->Max = Max(n->r);
  }
}
void push(node *n) {
  n->l->add += n->add;
  n->r->add += n->add;
  n->add = 0;
}
void build(node *n, int *a) {
  if (n->a == n->b) {
    n->Min = a[n->a];
    n->Max = a[n->a];
    n->pos = n->a;
    return;
  }
  int mid = (n->a + n->b) / 2;
  n->l = new node(n->a, mid);
  n->r = new node(mid + 1, n->b);
  build(n->l, a);
  build(n->r, a);
  pull(n);
}
void add(node *n, int l, int r, int k) {
  if (n->a >= l && n->b <= r) {
    n->add += k;
    return;
  }
  if (n->b < l || n->a > r) return;
  push(n);
  add(n->l, l, r, k);
  add(n->r, l, r, k);
  pull(n);
}
int main() {
  char c2[1000005];
  scanf("%s", c2 + 1);
  int ans = 1e9;
  for (int xx = 0; xx < 2; xx++) {
    char c[1000005];
    memset(c, 0, sizeof(c));
    for (int j = 1; c2[j] != 0; j++) c[j] = c2[j];
    int a[1000005];
    a[0] = 0;
    int n = strlen(c + 1);
    set<int> s;
    for (int i = 1; c[i] != 0; i++) {
      if (c[i] == '0')
        a[i] = a[i - 1] - 1;
      else
        a[i] = a[i - 1] + 1;
      if (c[i] == '?') s.insert(i), c[i] = '1';
    }
    root = new node(0, n);
    build(root, a);
    ans = min(ans, Max(root) - Min(root));
    vector<int> v;
    while (true) {
      int first = root->pos;
      if (s.upper_bound(first) == s.begin()) break;
      auto it = prev(s.upper_bound(first));
      c[*it] = '0';
      add(root, *it, n, -2);
      if ((Max(root) & 1) != xx) {
        v.push_back(*it);
      } else {
        if (v.size()) {
          add(root, v.back(), n, 2);
          c[v.back()] = '1';
          v.pop_back();
        }
      }
      ans = min(ans, Max(root) - Min(root));
      s.erase(it);
    }
  }
  printf("%d\n", ans);
}
