#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 5;
template <typename T>
void chkmax(T &x, T y) {
  x = max(x, y);
}
template <typename T>
void chkmin(T &x, T y) {
  x = min(x, y);
}
template <typename T>
void read(T &x) {
  x = 0;
  int f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-') f = -f;
  for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
  x *= f;
}
char s[MAXN];
int n, a[MAXN], sMin[MAXN], sMax[MAXN];
bool b[MAXN];
void init() {
  for (int i = 1; i <= n; i++) {
    if (s[i] == '0')
      a[i] = a[i - 1] - 1;
    else
      a[i] = a[i - 1] + 1;
    if (s[i] == '?') b[i] = true;
  }
  sMin[n + 1] = n + 5;
  for (int i = n; i >= 0; i--) sMin[i] = min(sMin[i + 1], a[i]);
}
int work(int limit) {
  int mns = 0;
  for (int i = 1; i <= n; i++) {
    if (s[i] == '?' && sMin[i] - mns - 2 >= limit) {
      b[i] = false;
      mns += 2;
    }
    a[i] -= mns;
  }
  sMin[n + 1] = n + 5;
  sMax[n + 1] = -n - 5;
  for (int i = n; i >= 0; i--) {
    sMin[i] = min(sMin[i + 1], a[i]);
    sMax[i] = max(sMax[i + 1], a[i]);
  }
  int ans = sMax[0] - sMin[0];
  int Max = 0, Min = 0;
  mns = 0;
  for (int i = 1; i <= n; i++) {
    if (b[i]) {
      mns -= 2;
      chkmin(ans, max(Max, sMax[i] - mns) - min(Min, sMin[i] - mns));
    }
    a[i] -= mns;
    chkmax(Max, a[i]);
    chkmin(Min, a[i]);
  }
  return ans;
}
int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  init();
  int ans = work(sMin[0]);
  init();
  chkmin(ans, work(sMin[0] - 1));
  cout << ans << endl;
  return 0;
}
