#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
inline void chmin(T1 &a, T2 b) {
  if (a > b) a = b;
}
template <typename T1, typename T2>
inline void chmax(T1 &a, T2 b) {
  if (a < b) a = b;
}
using Int = long long;
const char newl = '\n';
template <typename T, typename E>
struct SegmentTree {
  using F = function<T(T, T)>;
  using G = function<T(T, E)>;
  using H = function<E(E, E)>;
  Int n, height;
  F f;
  G g;
  H h;
  T ti;
  E ei;
  vector<T> dat;
  vector<E> laz;
  SegmentTree(F f, G g, H h, T ti, E ei) : f(f), g(g), h(h), ti(ti), ei(ei) {}
  void init(Int n_) {
    n = 1;
    height = 0;
    while (n < n_) n <<= 1, height++;
    dat.assign(2 * n, ti);
    laz.assign(2 * n, ei);
  }
  void build(const vector<T> &v) {
    Int n_ = v.size();
    init(n_);
    for (Int i = 0; i < n_; i++) dat[n + i] = v[i];
    for (Int i = n - 1; i; i--)
      dat[i] = f(dat[(i << 1) | 0], dat[(i << 1) | 1]);
  }
  inline T reflect(Int k) { return laz[k] == ei ? dat[k] : g(dat[k], laz[k]); }
  inline void propagate(Int k) {
    if (laz[k] == ei) return;
    laz[(k << 1) | 0] = h(laz[(k << 1) | 0], laz[k]);
    laz[(k << 1) | 1] = h(laz[(k << 1) | 1], laz[k]);
    dat[k] = reflect(k);
    laz[k] = ei;
  }
  inline void thrust(Int k) {
    for (Int i = height; i; i--) propagate(k >> i);
  }
  inline void recalc(Int k) {
    while (k >>= 1) dat[k] = f(reflect((k << 1) | 0), reflect((k << 1) | 1));
  }
  void update(Int a, Int b, E x) {
    if (a >= b) return;
    thrust(a += n);
    thrust(b += n - 1);
    for (Int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
      if (l & 1) laz[l] = h(laz[l], x), l++;
      if (r & 1) --r, laz[r] = h(laz[r], x);
    }
    recalc(a);
    recalc(b);
  }
  void set_val(Int a, T x) {
    thrust(a += n);
    dat[a] = x;
    laz[a] = ei;
    recalc(a);
  }
  T query(Int a, Int b) {
    if (a >= b) return ti;
    thrust(a += n);
    thrust(b += n - 1);
    T vl = ti, vr = ti;
    for (Int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
      if (l & 1) vl = f(vl, reflect(l++));
      if (r & 1) vr = f(reflect(--r), vr);
    }
    return f(vl, vr);
  }
  template <typename C>
  Int find(Int st, C &check, T &acc, Int k, Int l, Int r) {
    if (l + 1 == r) {
      acc = f(acc, reflect(k));
      return check(acc) ? k - n : -1;
    }
    propagate(k);
    Int m = (l + r) >> 1;
    if (m <= st) return find(st, check, acc, (k << 1) | 1, m, r);
    if (st <= l && !check(f(acc, dat[k]))) {
      acc = f(acc, dat[k]);
      return -1;
    }
    Int vl = find(st, check, acc, (k << 1) | 0, l, m);
    if (~vl) return vl;
    return find(st, check, acc, (k << 1) | 1, m, r);
  }
  template <typename C>
  Int find(Int st, C &check) {
    T acc = ti;
    return find(st, check, acc, 1, 0, n);
  }
};
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  string s;
  cin >> s;
  Int n = s.size();
  using P = pair<Int, Int>;
  auto f = [&](P a, P b) {
    return P(max(a.first, b.first), min(a.second, b.second));
  };
  auto g = [&](P a, Int b) { return P(a.first + b, a.second + b); };
  auto h = [&](Int a, Int b) { return a + b; };
  const Int INF = 1e9;
  P ti(-INF, INF);
  Int ei(0);
  SegmentTree<P, Int> seg(f, g, h, ti, ei);
  vector<P> vp(n + 1);
  Int sum = 0;
  vector<Int> qs;
  for (Int i = 0; i < n; i++) {
    vp[i] = P(i - sum * 2, i - sum * 2);
    if (s[i] == '?') qs.emplace_back(i);
    if (s[i] == '1') sum++;
  }
  vp[n] = P(n - sum * 2, n - sum * 2);
  seg.build(vp);
  Int ans = INF;
  auto check = [&]() {
    auto p = seg.query(0, n + 1);
    chmin(ans, p.first - p.second);
  };
  check();
  for (Int q : qs) {
    Int mi = seg.query(0, q + 1).second;
    auto p = seg.query(q + 1, n + 1);
    if (p.second - 2 >= mi) seg.update(q + 1, n + 1, -2);
    check();
  }
  cout << ans << endl;
  return 0;
}
