#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000010;
char s[maxn];
int n, ans, a[maxn], f[4][maxn];
inline int _abs(int x) { return x < 0 ? -x : x; }
int main() {
  scanf("%s", s + 3);
  n = strlen(s + 3) + 4;
  s[1] = s[n] = '0', s[2] = s[n - 1] = '1';
  for (int i = 1; i <= n; i++)
    a[i] = a[i - 1] + (s[i] == '?' ? 0 : (s[i] == '1' ? 1 : -1));
  for (int i = 1; i <= n; i++)
    f[0][i] = min(f[0][i], a[i]), f[1][i] = max(f[1][i], a[i]);
  for (int i = n; i; i--)
    f[2][i] = min(f[2][i], a[i]), f[3][i] = max(f[3][i], a[i]);
  for (int i = 1, cnt = 0; i < n; i++) {
    if (s[i] == '?') {
      cnt++;
      if (s[i + 1] == '?') continue;
      int x = f[0][i] + f[1][i], y = f[2][i + 1] + f[3][i + 1];
      int now = y - x;
      if (now > 2 * cnt)
        now = cnt;
      else if (now < -2 * cnt)
        now = -cnt;
      else
        now = (now - 2 * (cnt & 1) + 2) / 4 * 2 + (cnt & 1);
      f[0][i] += now, f[1][i] += now;
      cnt = 0;
    }
    f[0][i] = min(f[0][i - 1], f[0][i]), f[1][i] = max(f[1][i - 1], f[1][i]);
  }
  printf("%d\n", f[1][n] - f[0][n]);
  return 0;
}
