#include <bits/stdc++.h>
using namespace std;
const long long MOD = (long long)1e9 + 7;
const int SIZE = 2e5 + 5;
const long long INF = 1LL << 60;
const double eps = 1e-4;
const double PI = 3.1415926535897932;
char s[1000009];
int ps1[1000009], ps0[1000009];
int pmin1[1000009], pmin0[1000009];
int pc[1000009], nc[1000009];
int l0[1000009], l1[1000009], r0[1000009], r1[1000009];
int main() {
  scanf("%s", (s));
  int n = strlen(s);
  int ans = 1;
  for (int i = 0; i < (n); ++i) {
    ps1[i + 1] = ps1[i] + (s[i] == '1') - (s[i] != '1');
    ps0[i + 1] = ps0[i] + (s[i] == '0') - (s[i] != '0');
    pmin1[i + 1] = min(pmin1[i], ps1[i + 1]);
    pmin0[i + 1] = min(pmin0[i], ps0[i + 1]);
    l1[i + 1] = ps1[i + 1] - pmin1[i + 1];
    l0[i + 1] = ps0[i + 1] - pmin0[i + 1];
    if (s[i] != '?')
      pc[i + 1] = i + 1;
    else
      pc[i + 1] = pc[i];
    ans = max(ans, max(l1[i + 1], l0[i + 1]));
  }
  for (int i = n - 1; i >= 0; i--) {
    ps1[i + 1] = ps1[i + 2] + (s[i] == '1') - (s[i] != '1');
    ps0[i + 1] = ps0[i + 2] + (s[i] == '0') - (s[i] != '0');
    pmin1[i + 1] = min(pmin1[i + 2], ps1[i + 1]);
    pmin0[i + 1] = min(pmin0[i + 2], ps0[i + 1]);
    r1[i + 1] = ps1[i + 1] - pmin1[i + 1];
    r0[i + 1] = ps0[i + 1] - pmin0[i + 1];
    if (s[i] != '?')
      nc[i + 1] = i + 1;
    else
      nc[i + 1] = nc[i + 2];
    ans = max(ans, max(r1[i + 1], r0[i + 1]));
  }
  int aux = 0;
  for (int i = (1); i < (n + 1); ++i) {
    if (s[i - 1] != '?') continue;
    int left0 = l0[pc[i]], left1 = l1[pc[i]];
    int right0 = r0[nc[i]], right1 = r1[nc[i]];
    int len = nc[i] - pc[i] - 1;
    if (left0 == ans && right0 == ans && len % 2 != ans % 2) {
      aux = 1;
    }
    if (left1 == ans && right1 == ans && len % 2 != ans % 2) {
      aux = 1;
    }
  }
  printf("%d", ans + aux);
}
