#include <bits/stdc++.h>
using namespace std;
template <class T>
void _R(T &x) {
  cin >> x;
}
void _R(int &x) { scanf("%d", &x); }
void _R(int64_t &x) { scanf("%lld", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template <class T, class... U>
void R(T &head, U &...tail) {
  _R(head);
  R(tail...);
}
template <class T>
void _W(const T &x) {
  cout << x;
}
void _W(const int &x) { printf("%d", x); }
void _W(const int64_t &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template <class T, class U>
void _W(const pair<T, U> &x) {
  _W(x.first);
  putchar(' ');
  _W(x.second);
}
template <class T>
void _W(const vector<T> &x) {
  for (auto i = x.begin(); i != x.end(); _W(*i++))
    if (i != x.cbegin()) putchar(' ');
}
void W() {}
template <class T, class... U>
void W(const T &head, const U &...tail) {
  _W(head);
  putchar(sizeof...(tail) ? ' ' : '\n');
  W(tail...);
}
int MOD = 1e9 + 7;
void ADD(long long &x, long long v) {
  x = (x + v) % MOD;
  if (x < 0) x += MOD;
}
const int SIZE = 1 << 20;
char s[SIZE];
vector<vector<int> > AA;
int mark[SIZE];
int bb[SIZE];
void solve() {
  scanf("%s", (s));
  int n = strlen(s);
  int st = 0;
  while (st < n && s[st] == '?') st++;
  for (int i = st, j; i < n; i = j) {
    if (s[i] == '?') {
      mark[((int)(AA).size()) - 1]++;
      j = i + 1;
      continue;
    }
    int now = 0;
    int mi = 0, ma = 0;
    for (j = i; j < n && s[j] != '?'; j++) {
      if (s[j] == '0')
        now--;
      else
        now++;
      mi = min(mi, now);
      ma = max(ma, now);
    }
    AA.push_back({mi, ma, now});
  }
  for (int i = (1); i < (((int)(AA).size())); ++i) {
    if (bb[i - 1] + AA[i - 1][2] - AA[i - 1][0] - mark[i - 1] + AA[i][0] >= 0) {
      bb[i] = max(bb[i], bb[i - 1] + AA[i - 1][2] - AA[i - 1][0] - mark[i - 1] +
                             AA[i][0]);
    } else {
      if ((bb[i - 1] + AA[i - 1][2] - AA[i - 1][0] + mark[i - 1] + AA[i][0]) >=
          0) {
        bb[i] = max(bb[i], (bb[i - 1] + AA[i - 1][2] - AA[i - 1][0] +
                            mark[i - 1] + AA[i][0]) %
                               2);
      }
    }
  }
  for (int i = ((int)(AA).size()) - 2; i >= 0; i--) {
    if (bb[i + 1] - AA[i + 1][0] - mark[i] + AA[i][0] >= 0) {
      bb[i] = max(bb[i], bb[i + 1] - AA[i + 1][0] - mark[i] + AA[i][0]);
    } else {
      if ((bb[i + 1] - AA[i + 1][0] + mark[i] + AA[i][0]) >= 0) {
        bb[i] = max(bb[i], (bb[i + 1] - AA[i + 1][0] + mark[i] + AA[i][0]) % 2);
      }
    }
  }
  int an = 0;
  for (int i = 0; i < (((int)(AA).size())); ++i) {
    an = max(an, bb[i] + AA[i][1] - AA[i][0]);
  }
  W(an);
}
int main() {
  solve();
  return 0;
}
