#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i)
#define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i)

mt19937 Rnd(chrono::high_resolution_clock::now().time_since_epoch().count());

template <typename T>
inline void chkmax(T &x, T y) { if (x < y) x = y; }

template <typename T>
inline void chkmin(T &x, T y) { if (x > y) x = y; }

inline int read() {
  #define nc getchar()
  int x = 0;
  char c = nc;
  while (c < 48) c = nc;
  while (c > 47) x = x * 10 + (c ^ 48), c = nc;
  return x;
  #undef nc
}

typedef long long ll;
const int maxn = 1e6 + 10;
char str[maxn];
int n, A[maxn], B[maxn], tmp[maxn];

void solve() {
  scanf("%s", str + 1);
  n = strlen(str + 1);
  int mx = 0, pos = 0;
  rep(i, 1, n) {
    A[i] = A[i - 1] + (str[i] == '1' ? 1 : -1);
    if (mx <= A[i]) mx = A[i], pos = i;
  }
  memcpy(tmp, A, sizeof A);
  B[n] = A[n];
  per(i, n - 1, 1) B[i] = max(B[i + 1], A[i]);
  int s = 0;
  rep(i, pos, n) {
    if (str[i] == '?' && B[i] + s <= mx - 2) s += 2;
    A[i] += s;
  }
  int mn = 0;
  rep(i, 1, n) chkmin(mn, A[i]);
  int res1 = mx - mn;
  memcpy(A, tmp, sizeof A);
  s = 0;
  rep(i, pos, n) {
    if (str[i] == '?' && B[i] + s <= mx - 1) s += 2;
    A[i] += s;
  }
  mn = 0;
  rep(i, 1, n) chkmin(mn, A[i]);
  int res2 = mx - mn + 1;
  cout << min(res1, res2) << endl;
}

int main() {
//  int T = read();
//  while (T--) solve();
  solve();
  return 0;
}