#include <bits/stdc++.h>
const int maxn = 1000005;
char s[maxn];
int pre[maxn];
int sufmin[maxn], sufmax[maxn];
int shit(int n) {
  for (int i = 1; i <= n; i++) pre[i] = pre[i - 1] + (s[i] == '1' ? 1 : -1);
  sufmin[n] = sufmax[n] = pre[n];
  for (int i = n; i; i--) {
    sufmax[i - 1] = std::max(sufmax[i], pre[i - 1]);
    sufmin[i - 1] = std::min(sufmin[i], pre[i - 1]);
  }
  int premin = 0, premax = 0, res = 0;
  for (int i = 1; i <= n; i++) {
    res = std::max(res, sufmax[i] - premin);
    res = std::max(res, premax - sufmin[i]);
    premin = std::min(premin, pre[i]);
    premax = std::max(premax, pre[i]);
  }
  return res;
}
int begin[maxn];
int len[maxn];
char t[maxn];
char diff(char x) {
  if (!x or x == '0') return '1';
  return '0';
}
int main() {
  scanf("%s", s + 1);
  int n = int(strlen(s + 1));
  int p = 0;
  for (int l = 1, r; l <= n; l = r + 1) {
    r = l;
    while (r < n and s[r + 1] == s[l]) ++r;
    ++p;
    len[p] = r - l + 1;
    begin[p] = l;
    t[p] = s[l];
  }
  for (int i = 1; i <= p; i++)
    if (t[i] == '?') {
      int l = begin[i], r = begin[i] + len[i] - 1;
      int _i = i - 1, i_ = i + 1, x = 0;
      while (_i >= 1 and i_ <= n and len[_i] == len[i_]) --_i, ++i_, x ^= 1;
      if ((len[i - 1] > len[i + 1]) ^ x) {
        for (int j = l; j <= r; j++) s[j] = diff(s[j - 1]);
      } else {
        for (int j = r; j >= l; j--) s[j] = diff(s[j + 1]);
      }
    }
  printf("%d\n", shit(n));
}
