#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void out(T t) {
  cout << t << "\n";
}
template <class T, class... Ts>
inline void out(T t, Ts... ts) {
  cout << t << " ";
  out(ts...);
}
template <class T>
inline bool CHMIN(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool CHMAX(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
constexpr long long INF = 1e18;
template <typename T>
struct SegmentTree {
  using F = function<T(T, T)>;
  long long n;
  F f;
  T ti;
  vector<T> dat;
  SegmentTree() {}
  SegmentTree(F f, T ti) : f(f), ti(ti) {}
  void init(long long n_) {
    n = 1;
    while (n < n_) n <<= 1;
    dat.assign(n << 1, ti);
  }
  void build(const vector<T> &v) {
    long long n_ = v.size();
    init(n_);
    for (long long i = 0; i < n_; i++) dat[n + i] = v[i];
    for (long long i = n - 1; i; i--)
      dat[i] = f(dat[(i << 1) | 0], dat[(i << 1) | 1]);
  }
  void set_val(long long k, T x) {
    dat[k += n] = x;
    while (k >>= 1) dat[k] = f(dat[(k << 1) | 0], dat[(k << 1) | 1]);
  }
  T query(long long a, long long b) {
    if (a >= b) return ti;
    T vl = ti, vr = ti;
    for (long long l = a + n, r = b + n; l < r; l >>= 1, r >>= 1) {
      if (l & 1) vl = f(vl, dat[l++]);
      if (r & 1) vr = f(dat[--r], vr);
    }
    return f(vl, vr);
  }
  template <typename C>
  long long find(long long st, C &check, T &acc, long long k, long long l,
                 long long r) {
    if (l + 1 == r) {
      acc = f(acc, dat[k]);
      return check(acc) ? k - n : -1;
    }
    long long 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;
    }
    long long 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>
  long long find(long long st, C &check) {
    T acc = ti;
    return find(st, check, acc, 1, 0, n);
  }
};
void solve() {
  string s;
  cin >> s;
  long long N = s.size(), ma = 0;
  vector<long long> zsum(N + 1);
  for (long long i = 0; i < (long long)N; ++i) {
    if (s[i] == '0')
      zsum[i + 1] += zsum[i] + 1;
    else
      zsum[i + 1] += zsum[i] - 1;
  }
  SegmentTree<pair<long long, long long>> zseg(
      [](pair<long long, long long> a, pair<long long, long long> b) {
        return min(a, b);
      },
      {0, 0});
  zseg.build(vector<pair<long long, long long>>(N + 1));
  for (long long i = 0; i < (long long)N + 1; ++i) {
    zseg.set_val(i, {zsum[i], i});
  }
  for (long long i = 1; i < (long long)N + 1; ++i) {
    long long it = zseg.query(0, i).first;
    CHMAX(ma, zsum[i] - it);
  }
  vector<long long> osum(N + 1);
  for (long long i = 0; i < (long long)N; ++i) {
    if (s[i] == '1')
      osum[i + 1] += osum[i] + 1;
    else
      osum[i + 1] += osum[i] - 1;
  }
  SegmentTree<pair<long long, long long>> oseg(
      [](pair<long long, long long> a, pair<long long, long long> b) {
        return min(a, b);
      },
      {0, 0});
  oseg.build(vector<pair<long long, long long>>(N + 1));
  for (long long i = 0; i < (long long)N + 1; ++i) {
    oseg.set_val(i, {osum[i], i});
  }
  for (long long i = 1; i < (long long)N + 1; ++i) {
    long long it = oseg.query(0, i).first;
    CHMAX(ma, osum[i] - it);
  }
  vector<pair<pair<long long, long long>, long long>> v;
  for (long long i = 1; i < (long long)N + 1; ++i) {
    auto [val, idx] = zseg.query(0, i);
    if (zsum[i] - val == ma) {
      v.emplace_back(pair<long long, long long>(idx, i), 0);
    }
  }
  for (long long i = 1; i < (long long)N + 1; ++i) {
    auto [val, idx] = oseg.query(0, i);
    if (osum[i] - val == ma) {
      v.emplace_back(pair<long long, long long>(idx, i), 1);
    }
  }
  sort(v.begin(), v.end());
  long long flg = 0;
  for (long long i = 0; i < (long long)(long long)v.size() - 1; ++i) {
    if (v[i + 1].first.first <= v[i].first.second) continue;
    long long cnt = 0;
    long long z = 0;
    long long o = 0;
    if (v[i].second == 0)
      z += ma;
    else
      o += ma;
    if (v[i + 1].second == 0)
      z += ma;
    else
      o += ma;
    for (long long j = v[i].first.second; j < (long long)v[i + 1].first.first;
         ++j) {
      if (s[j] == '?') ++cnt;
    }
    if (v[i].second == v[i + 1].second && cnt % 2 == 0)
      flg = 1;
    else if (v[i].second != v[i + 1].second && cnt % 2)
      flg = 1;
  }
  out(ma + flg);
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long Q = 1;
  while (Q--) solve();
}
