#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;
template <class T>
inline bool setmin(T &a, T b) {
  if (a > b) return a = b, 1;
  return 0;
}
template <class T>
inline bool setmax(T &a, T b) {
  if (a < b) return a = b, 1;
  return 0;
}
template <class T>
inline T fast(T a, T b, T mod) {
  long long res = 1;
  while (b) {
    if (b & 1) res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}
template <class T>
istream &operator>>(istream &os, vector<T> &container) {
  for (auto &u : container) os >> u;
  return os;
}
template <class T>
ostream &operator<<(ostream &os, const vector<T> &container) {
  for (auto &u : container) os << u << " ";
  return os;
}
template <class T>
inline T gcd(T a, T b) {
  while (b) swap(a %= b, b);
  return abs(a);
}
const long long INF = 1e9 + 7;
const long long mod = 998244353;
const long long BIG_INF = 1e18 + 7;
const long long N = 1e6 + 7;
const long long T = 1 << 20;
const long double inf = 1e18;
long long m, n, k, q;
long long tree_min[T * 2];
long long lazy_min[T * 2];
void push_min(long long u) {
  tree_min[u] += lazy_min[u];
  if (u < T) {
    lazy_min[u * 2] += lazy_min[u];
    lazy_min[u * 2 + 1] += lazy_min[u];
  }
  lazy_min[u] = 0;
}
long long ask_min(long long pocz, long long kon, long long u = 1,
                  long long lo = 0, long long hi = T - 1) {
  push_min(u);
  if (kon < lo or hi < pocz) return INF;
  if (lo >= pocz and hi <= kon) return tree_min[u];
  long long mid = (lo + hi) / 2;
  return min(ask_min(pocz, kon, u * 2, lo, mid),
             ask_min(pocz, kon, u * 2 + 1, mid + 1, hi));
}
void add_min(long long pocz, long long kon, long long val, long long u = 1,
             long long lo = 0, long long hi = T - 1) {
  push_min(u);
  if (kon < lo or hi < pocz) return;
  if (lo >= pocz and hi <= kon) {
    lazy_min[u] += val;
    push_min(u);
    return;
  }
  long long mid = (lo + hi) / 2;
  add_min(pocz, kon, val, u * 2, lo, mid);
  add_min(pocz, kon, val, u * 2 + 1, mid + 1, hi);
  tree_min[u] = min(tree_min[u * 2], tree_min[u * 2 + 1]);
}
long long tree_max[T * 2];
long long lazy_max[T * 2];
void push_max(long long u) {
  tree_max[u] += lazy_max[u];
  if (u < T) {
    lazy_max[u * 2] += lazy_max[u];
    lazy_max[u * 2 + 1] += lazy_max[u];
  }
  lazy_max[u] = 0;
}
long long ask_max(long long pocz, long long kon, long long u = 1,
                  long long lo = 0, long long hi = T - 1) {
  push_max(u);
  if (kon < lo or hi < pocz) return -INF;
  if (lo >= pocz and hi <= kon) return tree_max[u];
  long long mid = (lo + hi) / 2;
  return max(ask_max(pocz, kon, u * 2, lo, mid),
             ask_max(pocz, kon, u * 2 + 1, mid + 1, hi));
}
void add_max(long long pocz, long long kon, long long val, long long u = 1,
             long long lo = 0, long long hi = T - 1) {
  push_max(u);
  if (kon < lo or hi < pocz) return;
  if (lo >= pocz and hi <= kon) {
    lazy_max[u] += val;
    push_max(u);
    return;
  }
  long long mid = (lo + hi) / 2;
  add_max(pocz, kon, val, u * 2, lo, mid);
  add_max(pocz, kon, val, u * 2 + 1, mid + 1, hi);
  tree_max[u] = max(tree_max[u * 2], tree_max[u * 2 + 1]);
}
void solve() {
  string second;
  cin >> second;
  long long pref = 0;
  for (long long i = 0; i < (long long)(second).size(); i++) {
    if (second[i] == '0')
      pref--;
    else
      pref++;
    add_max(i, i, pref);
    add_min(i, i, pref);
  }
  long long mini = ask_min(0, (long long)(second).size() - 1);
  for (long long i = 0; i < (long long)(second).size(); i++)
    if (second[i] == '?') {
      if (ask_min(i, (long long)(second).size() - 1) > mini + 1 or
          (ask_min(i, (long long)(second).size() - 1) == mini + 1 and
           ask_max(i, (long long)(second).size() - 1) > ask_max(0, i))) {
        add_max(i, (long long)(second).size() - 1, -2);
        add_min(i, (long long)(second).size() - 1, -2);
      }
    }
  cout << ask_max(0, (long long)(second).size() - 1) -
              ask_min(0, (long long)(second).size() - 1)
       << '\n';
}
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  q = 1;
  while (q--) solve();
}
