#include <bits/stdc++.h>
using namespace std;
const int MOD = (int)1e9 + 7;
const int FFTMOD = 119 << 23 | 1;
const int INF = (int)1e9 + 23111992;
const long long LINF = (long long)1e18 + 23111992;
const long double PI = acos((long double)-1);
const long double EPS = 1e-9;
inline long long gcd(long long a, long long b) {
  long long r;
  while (b) {
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}
inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
inline long long fpow(long long n, long long k, int p = MOD) {
  long long r = 1;
  for (; k; k >>= 1) {
    if (k & 1) r = r * n % p;
    n = n * n % p;
  }
  return r;
}
template <class T>
inline int chkmin(T& a, const T& val) {
  return val < a ? a = val, 1 : 0;
}
template <class T>
inline int chkmax(T& a, const T& val) {
  return a < val ? a = val, 1 : 0;
}
inline unsigned long long isqrt(unsigned long long k) {
  unsigned long long r = sqrt(k) + 1;
  while (r * r > k) r--;
  return r;
}
inline long long icbrt(long long k) {
  long long r = cbrt(k) + 1;
  while (r * r * r > k) r--;
  return r;
}
inline void addmod(int& a, int val, int p = MOD) {
  if ((a = (a + val)) >= p) a -= p;
}
inline void submod(int& a, int val, int p = MOD) {
  if ((a = (a - val)) < 0) a += p;
}
inline int mult(int a, int b, int p = MOD) { return (long long)a * b % p; }
inline int inv(int a, int p = MOD) { return fpow(a, p - 2, p); }
inline int sign(long double x) { return x < -EPS ? -1 : x > +EPS; }
inline int sign(long double x, long double y) { return sign(x - y); }
mt19937 mt(chrono::high_resolution_clock::now().time_since_epoch().count());
inline int mrand() { return abs((int)mt()); }
inline int mrand(int k) { return abs((int)mt()) % k; }
template <class num_t>
struct SegmentTree {
  int n;
  vector<pair<num_t, num_t>> st;
  vector<pair<num_t, num_t>> st2;
  vector<num_t> lz;
  void init(int n_) {
    n = n_;
    st.resize(n << 2);
    st2.resize(n << 2);
    lz.resize(n << 2);
  }
  void build(int p, int L, int R) {
    if (L == R) {
      st[p].second = st2[p].second = L;
      return;
    }
    build(p << 1, L, L + R >> 1);
    build(p << 1 | 1, (L + R >> 1) + 1, R);
  }
  void push(int p, int L, int R) {
    if (lz[p]) {
      st[p].first += lz[p];
      st2[p].first += lz[p];
      if (L < R) {
        lz[p << 1] += lz[p];
        lz[p << 1 | 1] += lz[p];
      }
      lz[p] = 0;
    }
  }
  void upd(int p, int l, int r, int L, int R, num_t val) {
    push(p, L, R);
    if (r < L || R < l) return;
    if (l <= L && R <= r) {
      lz[p] = val;
      push(p, L, R);
      return;
    }
    upd(p << 1, l, r, L, L + R >> 1, val);
    upd(p << 1 | 1, l, r, (L + R >> 1) + 1, R, val);
    st[p] = min(st[p << 1], st[p << 1 | 1]);
    st2[p] = max(st2[p << 1], st2[p << 1 | 1]);
  }
  void upd(int l, int r, num_t val) { upd(1, l, r, 0, n - 1, val); }
};
SegmentTree<int> seg;
void chemthan() {
  string s;
  cin >> s;
  int n = int((s).size());
  seg.init(n);
  seg.build(1, 0, n - 1);
  for (int i = (0); i < (n); ++i) {
    if (s[i] == '1') {
      seg.upd(i, n - 1, +1);
    } else {
      seg.upd(i, n - 1, -1);
    }
  }
  auto calc = [&]() {
    return max(0, seg.st2[1].first) - min(0, seg.st[1].first);
  };
  vector<int> fen(n + 1);
  auto upd = [&](int p, int v) {
    p++;
    for (; p < int((fen).size()); p += p & -p) {
      fen[p] += v;
    }
  };
  auto query = [&](int p) {
    p++;
    int res = 0;
    for (; 0 < p; p -= p & -p) {
      res += fen[p];
    }
    return res;
  };
  auto calc1 = [&](int x) {
    int lo = -1, hi = x - 1;
    int t = query(x - 1);
    while (lo < hi) {
      int mi = lo + hi + 1 >> 1;
      if (query(mi - 1) < t) {
        lo = mi;
      } else {
        hi = mi - 1;
      }
    }
    return lo + hi >> 1;
  };
  auto calc2 = [&](int x) {
    int lo = x + 1, hi = n;
    int t = query(x);
    while (lo < hi) {
      int mi = lo + hi >> 1;
      if (query(mi - 1) == t) {
        lo = mi + 1;
      } else {
        hi = mi;
      }
    }
    return lo + hi >> 1;
  };
  int res = calc();
  for (int i = (0); i < (n); ++i)
    if (s[i] == '?') {
      upd(i, 1);
    }
  while (1) {
    int t = seg.st[1].second;
    int t2 = calc1(t);
    if (t2 == -1) break;
    upd(t2, -1);
    seg.upd(t2, n - 1, 2);
    int t3 = calc2(t2);
    if (t3 < n) {
      upd(t3, 1);
      seg.upd(t3, n - 1, -2);
    }
    chkmin(res, calc());
  }
  cout << res << "\n";
}
int main(int argc, char* argv[]) {
  ios_base::sync_with_stdio(0), cin.tie(0);
  if (argc > 1) {
    assert(freopen(argv[1], "r", stdin));
  }
  if (argc > 2) {
    assert(freopen(argv[2], "wb", stdout));
  }
  chemthan();
  cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
  return 0;
}
